I am trying to set up a REST api
for my database using node js
and express
.
Now ive always been a fan of divide and conquer and because of this i get a bit upset about both the redundant code and massive server file you will get when creating a REST api in node j
s.
Take for example CRUD
operations on a user table:
// IMPORT ROUTES
// =============================================================================
var router = express.Router();
// on routes that end in /users
// ----------------------------------------------------
router.route('/user')
// create a user (accessed at POST http://localhost:8080/api/users)
.post(function (req, res) {
var username = req.body.username; //bodyParser does the magic
var password = req.body.password;
var user = User.build({username: username, password: password});
user.add(function (success) {
res.json({message: 'User created!'});
},
function (err) {
res.status(err).send(err);
});
})
// get all the users (accessed at GET http://localhost:8080/api/users)
.get(function (req, res) {
var user = User.build();
user.retrieveAll(function (users) {
if (users) {
res.json(users);
} else {
res.status(401).send("User not found");
}
}, function (error) {
res.status("User not found").send('user not found');
});
});
var User = sequelize.define('user', {
id: DataTypes.INTEGER,
username: DataTypes.STRING,
password: DataTypes.STRING,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
type_id: DataTypes.INTEGER,
join_date: DataTypes.STRING,
image_path: DataTypes.STRING,
status_id: DataTypes.INTEGER
}, { freezeTableName: true,
instanceMethods: {
retrieveAll: function (onSuccess, onError) {
User.findAll({}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function (user_id, onSuccess, onError) {
User.find({where: {id: user_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (onSuccess, onError) {
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.build({username: username, password: password})
.save().ok(onSuccess).error(onError);
},
updateById: function (user_id, onSuccess, onError) {
var id = user_id;
var username = this.username;
var password = this.password;
var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');
User.update({username: username, password: password}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (user_id, onSuccess, onError) {
User.destroy({where: {id: user_id}}).success(onSuccess).error(onError);
}
}
}
);
// on routes that end in /users/:user_id
// ----------------------------------------------------
router.route('/users/:user_id')
// update a user (accessed at PUT http://localhost:8080/api/users/:user_id)
.put(function (req, res) {
var user = User.build();
user.username = req.body.username;
user.password = req.body.password;
user.updateById(req.params.user_id, function (success) {
console.log(success);
if (success) {
res.json({message: 'User updated!'});
} else {
res.send(401, "User not found");
}
}, function (error) {
res.send("User not found");
});
})
// get a user by id(accessed at GET http://localhost:8080/api/users/:user_id)
.get(function (req, res) {
var user = User.build();
user.retrieveById(req.params.user_id, function (users) {
if (users) {
res.json(users);
} else {
res.status(401).send("User not found");
}
}, function (error) {
res.send("User not found");
});
})
// delete a user by id (accessed at DELETE http://localhost:8080/api/users/:user_id)
.delete(function (req, res) {
var user = User.build();
user.removeById(req.params.user_id, function (users) {
if (users) {
res.json({message: 'User removed!'});
} else {
res.status(401).send("User not found");
}
}, function (error) {
res.send("User not found");
});
});
Now this is only for one table.
So i thought there must be a better way to organize all of this?
So my question is can you divide each route into a separate file and is there a way to simplify the routing / collecting of data so you remove the redundancy?
This is how I do it:
//controllers/someController.js
var express = require('express');
var router = express.Router();
router.post('/something', function(req, res, next) {
...
});
router.get('/something', function(req, res, next) {
...
});
module.exports = router;
//server.js
var app = require('express')();
var someController = require('./controllers/someContoller');
app.use('/some', someController);
So basically I create a middleware that will handle the request at specified path. You can even simplify this process by iterating through all controller files that you have and requiring them, but I like it to be like that :)
Update:
You can pass dependencies right to controller:
//someController.js
module.exports = function(express) {
var router = express.Router();
router.get('', function() {});
return router;
}
//server.js
var app = require('express')();
var someController = require('./controllers/someContoller')(express);
app.use('/some', someController);