How should I implement a RESTful API with a web frontend in Node.js?

3.3k Views Asked by At

I have a setup consisting of multiple servers (written in Java) that need to communicate towards a central server, posting status updates every so often. These status updates will get written to a database, probably MongoDB/Mongoose which will be handled by the web end via REST.

I have been looking at Restify and Express as two ways to approach this problem. The website will query the database as well as the REST api.

How should I approach this? Should I use both Restify and Express to create a website with an API? Should I use Railway? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

It doesn't make sense to use a complex framework such as Railway, which is built on top of Express and tries to resemble Ruby on Rails.

You should either choose Express or Restify, not both.

I would pick Express over Restify because the code is excellently documented and the library is more mature and heavily used. You can find a bunch of useful tutorials on how to make such apps with Express, and the API is great:

var express = require('express')
  , app = express.createServer();

var users = [{ name: 'tj' }];

app.all('/user/:id/:op?', function(req, res, next){
  req.user = users[req.params.id];
  if (req.user) {
    next();
  } else {
    next(new Error('cannot find user ' + req.params.id));
  }
});

app.get('/user/:id', function(req, res){
  res.send('viewing ' + req.user.name);
});

app.get('/user/:id/edit', function(req, res){
  res.send('editing ' + req.user.name);
});

app.put('/user/:id', function(req, res){
  res.send('updating ' + req.user.name);
});

app.get('*', function(req, res){
  res.send('what???', 404);
});

app.listen(3000); 
1
On

I personally find express.js to suit my needs because of it's routing functionality that is great. Check out http://expressjs.com/guide.html#routing. It gets the job done for a RESTful api and is extremely fast.

also: Node-PerfectAPI vs Restify.js vs ExpressJS vs Node-APIServer