A simple http server built on top of express. This one makes it easy to create Rest APIs.
$ npm install ol
After requiring the module, you just need to specify the routes path and the controllers directory, Assuming you have this project structure:
.
├── app.js
├── routes.json
└── controllers
└── TestController
To specify the path to routes file and controllers directory :
var Ol = require('ol');
var ol = Ol({
routes : "./routes.json",
controllers : "/ontrollers"
});
The ol object is actually the express app, you just need to make it listen on a specific port :
ol.listen('3000', function () {
console.log(" Listening ... ! ");
});
You also can pass the path to a json file to the Ol
constructor. This file should contain the path to routes.json and the controllers directory.
params.json :
{
"routes" : "./routes.json",
"controllers" : "/ontrollers"
}
app.js :
var Ol = require('ol');
var ol = Ol('./params.json');
Example of routes.json:
[
{
"method" : "GET",
"route" : "/",
"controller" : "TestController",
"action" : "sayHi"
},
{
"method" : "POST",
"route" : "/testpost",
"controller" : "TestController",
"action" : "anotherAction"
}
]
Example of a controller (TestController)
module.exports = {
sayHi : function (req, res) {
res.json({
foo : bar
});
},
anotherAction : function (req, res) {
res.send(req.params);
}
}