Mikrop is a rapid api development library based on restify.
Mikrop contain common methods and packages for required every micro service.
installing:
npm i --save mikrop
const mikrop = require('mikrop');
// default config
const config = {
apiDir: `${process.cwd()}/api`,
requiredVariables: [],
useRequestLogger: true,
useAuditLogger: true,
useMongo: false,
dateParser: 60,
server: ServerOptions,
throttle: ThrottleOptions,
bodyParser: BodyParserOptions,
queryParser: QueryParserOptions,
cpuUsageThrottle: CpuUsageThrottleOptions,
};
mikrop.run(config); // mikrop listening at http://[::]:3000
Absolute path of where contain your api handlers and config files.
Array of required env variable for working your service.
If you want to see request logs on terminal, it's must be true.
Detailed error log for monitoring.
If it's true and you have a valid env called MONGO_URI, service connect to database when starting.
Folder structure is most important thing for Mikrop.
Your folder structure must be as shown below:
my-microservice
|-- api -> This folder path can change with configuration object.
|--user
|--index.js -> Contain route handler methods.
|--config.json -> Contain path and methods.
|-- lib
|-- index.js
...etc
iapi/user/ndex.js:
module.exports.getUser = (req, res, next) => {
res.send('Get user');
};
module.exports.updateUser = (req, res, next) => {
res.send('Update user');
};
module.exports.formValidation = (req, res, next) => {
res.send('Form validation');
};
api/user/config.json:
{
"name": "User api",
"description": "Sample user api description",
"routes": {
"/user": {
"get": {
"handlers": ["getUser"],
"public": true
}
},
"/user/:id": {
"put": {
"handlers": ["formValidation", "updateUser"]
}
}
}
}
index.js:
const mikrop = require('mikrop');
mikrop.run(); // mikrop listening at http://[::]:3000
mikrop wants some env variable for running.
NAME=Service Name
PORT=3000
URL=localhost
NODE_ENV=development
You can add more required env variable for specific services.
Configuration:
const mikrop = require('mikrop');
const options = {
requiredVariables: [
'MY_REQUIRED_ENV_VARIABLE',
],
},
mikrop.run(options); // mikrop listening at http://[::]:3000