express.Router() vs. app.get

后端 未结 4 2048
走了就别回头了
走了就别回头了 2020-12-31 10:22

I am using Express 4 server for Node.js

There is a router baked into Express like so:

in app.js

    var router =         


        
4条回答
  •  鱼传尺愫
    2020-12-31 11:07

    Express().get does the same as Express.Router().get. The difference is that router is better practice because it allows us to manage api endpoints as a middleware.

    https://expressjs.com/en/api.html#router

    The problem with Express and JS frameworks like it is that the documentation doesn't explain why the shift from using the Express main "app" object. My guess is that we should separate concerns from the logic of routes from the main application management itself (ie. configuration, template, database connection, etc.)

提交回复
热议问题