Fireship Fireship - 4 months ago 7x
Node.js Question

Including API routes in Express 4.x

I'm learning MEAN stack with 'Getting MEAN with...' book, and problem is older Express version in books than i use.

The first step is to tell our application that we’re adding more routes to look out for,
and when it should use them. We already have a line in app.js to require the server
application routes, which we can simply duplicate and set the path to the API routes
as follows:

var routes = require('./app_server/routes/index');
var routesApi = require('./app_api/routes/index');

Next we need to tell the application when to use the routes. We currently have the following line in app.js telling the application to check the server application routes for
all incoming requests:

app.use('/', routes);

Notice the '/' as the first parameter. This enables us to specify a subset of URL s for
which the routes will apply. For example, we’ll define all of our API routes starting
with /api/ . By adding the line shown in the following code snippet we can tell the application to use the API routes only when the route starts with /api :

app.use('/', routes);
app.use('/api', routesApi);

And there's listing of my app.js file:

var express = require('express')
, others = require('./app_server/routes/others')
, locations = require('./app_server/routes/locations')
, routesApi = require('/app_api/routes/index')
, ;


var app = module.exports = express.createServer();

// Configuration

app.set('views', __dirname + '/app_server/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

app.configure('production', function(){

// Routes
app.get('/', locations.homeList);
app.get('/location', locations.locInfo);
app.get('/location/review/new', locations.addReview);
app.get('/about', others.about);

app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

Can someone explain me how to do the same in my Express version ?


In Express 4, this is done using Router Middleware. More info is available on Express Routing here.

A Router is simply a mini express app that you can define middleware and routes on that should all be packaged together, ie /api should all use apiRouter. Here is what apiRouter could look like


var express = require('express')
var router = express.Router(); // Create our Router Middleware

// GET / route
router.get('/', function(req, res) {
    return res.status(200).send('GET /api received!');

// export our router middleware
module.exports = router;

Your main Express app would stay the same, so you would add your router using a require() to import the actual file, and then inject the router with use()

Express Server File

var express = require('express');
var app = express();
var apiRouter = require('../apiRouter');

var port = process.env.PORT || 3000;

app.use('/', apiRouter);

app.listen(port, function() {
  console.log('listening on ' + port);