Skip to content

Instantly share code, notes, and snippets.

@natmegs natmegs/app.js
Created Aug 26, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.