@maf/rest-service
v0.20.0
Published
maf rest service
Downloads
7
Readme
@maf/rest-service
install
npm i --save @maf/rest-service@latest
# OR
yarn add @maf/rest-service@latest
usage
simple
require('@maf/rest-service')('simple-service', {port: 4000})
.setEndpoint('/api/v1')
.addMethods({
'GET /todos': (req, res) => {
req.logger.debug(`req.id = ${req.ctx.id}`);
res.result([1, 2, 3]);
}
})
.start();
// curl -v http://localhost:4000/api/v1/todos
with request validation
const service = require('@maf/rest-service')('myservice');
service.setEndpoint('/api/v1');
const joi = service.joi;
service.addMethods({
'GET /todos': {
schema: {
query: joi.object().keys({
limit: joi.number().integer().min(1).max(5).default(5)
})
},
handler: (req, res) => {
const todos = [1, 2, 3, 4, 5];
res.result(todos.slice(0, req.query.limit));
}
}
});
service.start();
// curl -v http://localhost:3000/api/v1/todos
// curl -v http://localhost:3000/api/v1/todos?limit=3
// curl -v http://localhost:3000/api/v1/todos?limit=10