Skip to content

Instantly share code, notes, and snippets.

@natmegs
Created August 26, 2017 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natmegs/fda851a0b2d031248ac588ee7553c268 to your computer and use it in GitHub Desktop.
Save natmegs/fda851a0b2d031248ac588ee7553c268 to your computer and use it in GitHub Desktop.
Express example using Router
var express = require('express');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
var port = process.env.PORT || 3000;
// path matching / will be handled by routes/index.js.
// index.js is set up with a '/something' subroute which can
// be accessed now with /something ,
// as well as a '/' subroute which can be accessed with '/'
app.use('/', routes);
// path matching /users/ will be handled by routes/users.js.
// Set a path '/users' and then within that path, the routes are processed.
// Basically, it takes path matching '/user/(whatever the matching routes are in users.js router)'.
// Paths defined in users.js router are the users/ subroutes.
// In this example, users is only set up with '/' path so
// it will respond to paths matching '/users/'
app.use('/users', users);
app.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment