Easily map routes to request handlers.
/* server.js */
const { ponaserv } = require('ponaserv');
const express = require('express');
const path = require('path');
const app = express();
ponaserv(app, { services: path.join(__dirname, 'services') });
app.listen(3000, () => {
console.log('App listening on port 3000');
});
/* services/hello.js */
const express = require('express');
module.exports = {
name: 'hello-world',
routes: {
'GET /': 'sayHello',
'GET /:message': 'specialHello',
'POST /': 'jsonIsCool',
'GET /math': 'math',
},
actions: {
sayHello(req, res) {
res.send('Hello World!');
},
specialHello(req, res) {
const { message } = req.params;
res.send(message.toUpperCase());
},
jsonIsCool: {
middleware: [express.json()],
handler(req, res) {
const { body } = req;
res.send({ yourData: body });
}
},
math: {
middleware: [express.json()],
params: {
a: 'number',
b: 'number',
$$strict: true,
},
handler(req, res) {
const params = {...req.params, ...req.body, ...req.query};
res.send(this.calc(params.a, params.b));
}
}
},
methods: {
calc(a, b) {
return a + b;
}
}
}
Head to our documentation to learn more about ponaserv.
Ponaserv is MIT licensed.