const http = require("http");
const rest = require("micro2-rest");
rest.use('/', {
get: function(req, res, done) {
done({
url: req.url,
method: req.method,
});
},
post: function(req, res, done) {
}
}, (err, req, res, next) => {
if (err) res.end(err);
next();
});
rest.use('/post/:id(\\d+)', {
get: function(req, res, done) {
done({
url: req.url,
method: req.method,
data: {
a: 10
}
});
},
post: function(req, res, done) {
done({
url: req.url,
method: req.method,
});
}
}, (err, req, res, next) => {
if (err) res.end(err);
next();
});
const server = http.createServer(function(req, res) {
rest()(req, res);
});
server.listen(3000);
micro2-rest is middleware for serve rest api request.