This is a simple Node.js module for configuration express router using MVC code style.
Using npm:
$ npm install node-api-router --save
Using latest version on github.com (only for testing)
$ npm install https://github.com/andryuha49/node-api-router --save
Create simple controller and to app configuration
app:
import express from 'express';
import http from 'http';
import bodyParser from 'body-parser';
import {UsersController} from './controllers/usersController';
const app = express();
const server = http.createServer(app);
app.use(bodyParser.json({ limit: '50mb' })); // Parse application/json
app.use('/api/v1', new UsersController());
const port = process.env.PORT || 3001;
server.listen(port, () => {
console.log('[Express] Api is running on port', port);
});
controller
import {router, route} from 'node-api-router';
const globalMiddleware = (req, res, next) => {
return next();
};
const methodMiddleware = (req, res, next) => {
return next();
};
@router('/users', globalMiddleware)
export class UsersController {
@route('/', 'get')
getAll(req, res) {
return res.status(200).json([{id: 1, login:'user1'},{id: 2, login:'user2'}]);
}
@route('/:id', 'get', methodMiddleware)
getById(req, res) {
const id = req.params.id;
return res.status(200).json({id: id, login:'user ' + id});
}
@route('/:id', 'put')
put(req, res) {
return res.status(200).json({message:'ok'});
}
@route('/', 'post')
post(req, res) {
return res.status(200).json({message:'ok'});
}
@route('/:id', 'delete')
delete(req, res) {
return res.status(200).json({message:'ok'});
}
}
Also available to use custom decorators
import {router, httpGet, httpPost, httpPut, httpDelete} from 'node-api-router';
@router('/users')
export class UsersController {
@httpGet('/')
getAll(req, res) {
return res.status(200).json([{id: 1, login:'user1'},{id: 2, login:'user2'}]);
}
@httpGet('/:id')
getById(req, res) {
return res.status(200).json({id: 1, login:'user1'});
}
@httpPut('/:id')
put(req, res) {
return res.status(200).json({message:'ok'});
}
@httpPost('/')
post(req, res) {
return res.status(200).json({message:'ok'});
}
@httpDelete('/:id')
delete(req, res) {
return res.status(200).json({message:'ok'});
}
}
If set route decorator without any parameters you can use route by method name
@route()
getAllUsers(req, res) {
const users = this.usersRepository.getAll();
return res.status(200).json(users);
}
You can use GET method by URL
http://localhost:3001/api/v1/users/getAllUsers
Request parameters in the router:
import {router, route} from 'node-api-router';
@router('/users/:userId/photos')
export class UsersController {
/*
* Get all user photos
*/
@route('/', 'get')
getAll(req, res) {
const userId = req.params.userId;
return res.status(200).json([{userId: userId, photoId: 1},{userId: userId, photoId: 2}]);
}
/*
* Get user photo by photo id
*/
@route('/:photoId', 'get')
getById(req, res) {
const userId = req.params.userId;
const photoId = req.params.photoId;
return res.status(200).json({userId: userId, photoId: photoId});
}
}
Available ES6 example in this folder