
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Rest-lib is a light-weight framework for building ReSTful APIs
$ npm install rest-lib
const ioc = require('nan-ioc');
const restlib = require('rest-lib');
class Application {
onInitialized() {
this.expressServer
.run()
.then(() => console.log("Server running"));
}
}
class HelloController {
list() {
return "Hello world";
}
}
ioc.module([restlib.core])
.controller('helloController', {
'class': HelloController,
'context': '/hello'
})
.component('application', {
class: Application,
expressServer: ioc.ref('expressServer')
})
.build({
logger: {
level: 'INFO'
}
});
Start the app:
node .
Make a request:
curl localhost:3000/hello
To start off you will need to define an Application.js and an index.js
var Q = require('q');
module.exports = App;
App.prototype.onInitialized = function(){
var bootstrap = [this.syncDb];
boostrap.push(this.expressServer.run.bind(this,expressServer));
bootstrap.reduce(Q.when, Q(true));
}
App.prototype.syncDb = function(){
return this.dataSource.sync();
};
A module is a group of components with related behavior. You can define a module adding an index file to it like:
var ioc = require('nan-ioc');
var Service = require('Service');
var Controller = require('Controller');
var DBModel = require('DBModel');
ioc.module('myModule')
.service('service',{
class: Service
})
.controller('controller',{
context: '/hello',
class: Controller,
service: ioc.ref('service')
})
.model('dbModel',{
class: DBModel
});
Note: You can have several of each (as long as they have a different definition name), you can reuse your components
A module can have:
The methods that you can define on a controller to route through XHR requests are:
Note: you don't need to define the id, rest-lib will read it automaticly, you can access it from the request object that your method receives
module.exports = HelloController;
var i = 0;
function HelloController() {
}
HelloController.prototype.list = function (command) {
return {
'message': this.message + ' ' + command.name
};
};
HelloController.prototype.create = function (command) {
return this.helloService.create(command);
};
For example, for the user context you can have:
Sometimes, you need add a nested routes. For example, with posts and comments you need a endpoint that returns all the comments of one post. So, you may define adding in the index file of your module:
ioc.module('myModule')
.controller('postController',{
context: '/post',
routes: {
posts: {
verb: 'get',
route: ':postId/comment'
}
},
class: PostController
});
The code above will generate 'GET /post/:postId/comment' and you need to define a comments method in your post controller.
Also, you can need define a set of methods for the nested routes. For example, following the post and comment context:
So, you can use the index file of the comment module and put the following code:
ioc.module('myModule')
.controller('commentController',{
context: '/post/:postId/comment',
},
class: CommentController
});
There are four differents ways of define params in the validators:
When you define the validator you can specify what type of params you must receive in the controller:
var Joi = require('joi');
module.exports = {
'update': {
params: {
id: Joi.number().required()
},
body: {
name: Joi.string().required()
}
},
'list': {
query: {
name: Joi.string().required()
}
},
'create': {
body: {
name: Joi.string().required()
}
}
};
Note: If you define a validator for a controller, the methods validated will receive a Command object before the Request and Response objects, in this example the list method would receive an object like:
{
name: 'Jon'
}
var transactional = require('../').transactional;
module.exports = HelloService;
function HelloService(){
}
HelloService.prototype.create = transactional({inherit: true, isolationLevel: 'READ COMMITTED'}, function(project){
return this.model.create(project)
});
var jwt = require('jsonwebtoken');
module.exports = class SessionMiddleware{
get(){
return function(request, response, next){
var user = request.get('user');
this.userService.verifyUser(user).then(function(){
next();
}).catch(next);
}.bind(this);
}
}
var Sequelize = require('sequelize');
module.exports = {
modelName: 'Project',
attributes: {
name: {
type: Sequelize.STRING
}
},
afterDefined: function(models){
this.belongsToMany(models.User, {through: 'users_project'});
}
};
By default, rest-lib includes a set of mixins for your middlewares, services and controllers. For example, all controller methods throw a not implemented (501) error instead of a 404.
You can set up a json file for your settings and add them to the index build, as seen in the example [index.js](## Application) file.
If an authentication field is found, rest-lib will add the express-jwt middleware to verify the requests. If you want your route to not be protected you need to add it in an array in the unsecuredUris field, like:
authentication: {
unsecuredUris: ['/auth']
}
Note: You will still need to sign your own tokens
You can set up a connection using Sequelize as seen in the example,
var ioc = require('nan-ioc');
var UserModel = require('./User');
var ProjectModel = require('./Project');
var App = require('./App');
ioc.module(['../lib/model'])
.model('User', {
'class': UserModel
})
.model('Project', {
'class': ProjectModel
})
.component('app',{
'class': App,
'env': '${env}',
'dataSource': ioc.ref('dataSource'),
'Project': ioc.ref('Project'),
'User': ioc.ref('User')
})
.build({
env: 'dev',
server:{
port: 9000
},
db:{
database: 'restlib',
username: 'restlib',
password: 'restlib',
sync: {force: true},//see http://docs.sequelizejs.com/en/latest/api/sequelize/#syncoptions-promise
options: {
host: 'localhost',
port: '3306',
logging: console.log
}
}
});
Note: You need to inject dataSource into your Application, you can also do this directly on the Application definition (main index.js file)
FAQs
REST library
The npm package rest-lib receives a total of 2 weekly downloads. As such, rest-lib popularity was classified as not popular.
We found that rest-lib demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.