Created
August 26, 2017 21:35
-
-
Save natmegs/fda851a0b2d031248ac588ee7553c268 to your computer and use it in GitHub Desktop.
Express example using Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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