Sub-routes
Separate files for routes
Define a router
- Create folder
routes/
- Create file
articles.js
- import
express.Router()
express router
has same capabilities asexpress app
but cannot listen to port- Add routes
- export router
articles.js
const express = require('express')
const router = express.Router()
// is equivalent to '/articles'
router.get('/', (req, res) => {
res.send('Articles!')
})
// is equivalent to '/articles/:id'
router.get('/:id', (req, res) => {
res.send(`Article ${req.params.id}`)
})
module.exports = router
Register route in main router
- Import routes in
app.js
- Register
/articles
route usingapp.use()
app.js
const articleRoutes = require('./routes/articles')
// Middleware
app.use('/articles', articleRoutes)
Router Verb Methods
- Use
router.route()
method and chainget()
,post()
,put()
anddelete()
for clean code structure
articles.js
const express = require('express')
const router = express.Router()
// /articles
router
.route('/')
.get((req, res) => {
res.send('Get All Articles!')
})
.post((req, res) => {
res.send('Create new Article!')
})
// /article/id
router
.route('/:id')
.get((req, res) => {
res.send('Get Article by Id!')
})
.put((req, res) => {
res.send('Update Article by Id!')
})
.delete((req, res) => {
res.send('Delete Article by Id!')
})
module.exports = router