express.Router() vs. app.get

后端 未结 4 2062
走了就别回头了
走了就别回头了 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 10:48

    Great question but nobody should need to ask this question.

    The truth is express is an excellent framework. But I have seen so many questions about route handling that we all wasted time. And also even when done correctly, it is just a heap of code that actually wasted people's time writing it and even more in correct stupid errors.

    Try Route Magic

    You want to do just 2 lines of code and have the module read your directory and file structure to handle all the app.use routing invocations, like this:

    const magic = require('express-routemagic')
    magic.use(app, __dirname, '[your route directory]')
    

    That's really it! Nothing else.

    For those you want to handle manually, just don't use pass the directory to Magic.

    Say goodbye to

    app.use('/i/repeat/myself', require('./routes/i/repeat/myself'))
    app.use('/i/repeat/myself/again', require('./routes//i/repeat/myself/again'))
    app.use('/i/keep/repeating/myself', require('./routes/i/keep/repeating/myself'))
    

提交回复
热议问题