简体   繁体   中英

Why express.Router() while separating routes

  1. I have gone through express document in that i have learn about Router() and express.Router(). so my question is i have separated my all routes from main app and created different folder there i did not create any router object(var router express.Router()) for routing to specific path still it's working fine. So i want to know why this Router class is necessary ?

    see, This is main app file,

    'use strict'; const startUpDebugger=require('debug')('app:startUp'); const dbDebugger=require('debug')('app:db'); const express = require('express'); const app = express(); const moragan=require('morgan'); const helmet=require('helmet'); const config=require('config'); const courses=require('./routes/courses'); const home=require('./routes/home'); app.use(helmet()); app.set('view engine','pug'); app.set('views','./view'); app.use(express.json()); app.use('/api/courses',courses); app.use('/',home);

    console.log( Node enironment variable: ${process.env.NODE_ENV} ); console.log( Application name : ${config.get('name')} ); console.log( mail server : ${config.get('mail.host')} );

    if(app.get('env')==='development'){ app.use(moragan('tiny')); startUpDebugger("******Morgan enabled*******") }

    const port = process.env.PORT || 5000; app.listen(port);

    console.log( Api Node running on port ${port} );

    This is my courses.js which is my route file

    const express=require('express'); const app=express

    const courses=[{"id":1,"course":"course1"}, {"id":2,"course":"course2"}, {"id":3,"course":"course3"}, {"id":4,"course":"course4"}]

    app.route('/posting').post((req,res)=>{ console.log(req.body); courses.push(req.body) res.status(201).send(courses); }).put((req,res)=>{ res.send("Successfully put message") }) app.get('/sub/:id',(req,res)=>{ res.status(200).send(req.params); })

    module.exports=app;

your question is not clear but if i am getting you right:-

If your app is really simple, you don't need routers.

//you can just invoke express constructor and use it   
const app=express() 
app.get('/', function (req, res) {
  res.send('root')
}) // app.get,app.post and so on  

in your main app file.

But as soon as it starts growing, you'll want to separate it into smaller "mini-apps", so that it's easier to test and maintain, and to add stuff to it. You'll avoid a huge main app file :) .
so here you need to make another file for routes and have to invoke express.Router() class and use it like:

//route.js
const Router=express.Router()

    // middleware that is specific to this router
    router.use(function timeLog (req, res, next) {
      console.log('Time: ', Date.now())
      next()
    })
    // define the home page route
    router.get('/', function (req, res) {
      res.send('Birds home page')
    })
    // define the about route
    router.get('/about', function (req, res) {
      res.send('About birds')
    })  

And import this file into the main app file and pass it to middleware to use

app.use('/',require('./routes/routes.js'))  

and comparing to express() object the express.Router() object is light weight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM