admin管理员组文章数量:1276882
I am using Express 4 server for Node.js
There is a router baked into Express like so:
in app.js
var router = express.Router();
app.use(router);
app.use('/users', usersRoutes);
in userRoutes.js:
var router = express.Router();
router.get('/', function (req, res, next) {
}
router.get('/:user_id', function (req, res, next) {
}
router.post('/:user_id', function (req, res, next) {
}
router.put('/:user_id', function (req, res, next) {
}
router.delete('/:user_id', function (req, res, next) {
}
module.exports = router;
but I am finding it very difficult to find any solid documentation for this type of router online. There is a lot more documentation for the old style of using app.get, app.post, app.put, app.delete, etc. One of the more confusing things is that the first argument (the route path) seems to require that we as programmers strip the app.use argument from the router.get/post/put/delete methods.
For example:
app.use('/users', usersRoutes);
...this means that all the routes in usersRoutes already have an invisible '/users'
at the beginning of the paths - something I am not sure I like yet.
This means in usersRoutes.js:
var router = express.Router();
router.get('/users/:user_id', function (req, res, next) { //WRONG!!
}
router.get('/:user_id', function (req, res, next) { //RIGHT
}
This is a bit confusing, but perhaps something I could appreciate with longer paths.
Given the lack of documentation for this express.Router - I assume this is not the preferred way - but is it possible to create a solid RESTful backend with express.Router - and does it have all the basic HTTP verbs attached to it?
Another confusing thing is ----> in app.js we have an instance of router app.use(express.Router())
- how does this router instance interact with the others? Makes little sense on the face of it.
I am using Express 4 server for Node.js
There is a router baked into Express like so:
in app.js
var router = express.Router();
app.use(router);
app.use('/users', usersRoutes);
in userRoutes.js:
var router = express.Router();
router.get('/', function (req, res, next) {
}
router.get('/:user_id', function (req, res, next) {
}
router.post('/:user_id', function (req, res, next) {
}
router.put('/:user_id', function (req, res, next) {
}
router.delete('/:user_id', function (req, res, next) {
}
module.exports = router;
but I am finding it very difficult to find any solid documentation for this type of router online. There is a lot more documentation for the old style of using app.get, app.post, app.put, app.delete, etc. One of the more confusing things is that the first argument (the route path) seems to require that we as programmers strip the app.use argument from the router.get/post/put/delete methods.
For example:
app.use('/users', usersRoutes);
...this means that all the routes in usersRoutes already have an invisible '/users'
at the beginning of the paths - something I am not sure I like yet.
This means in usersRoutes.js:
var router = express.Router();
router.get('/users/:user_id', function (req, res, next) { //WRONG!!
}
router.get('/:user_id', function (req, res, next) { //RIGHT
}
This is a bit confusing, but perhaps something I could appreciate with longer paths.
Given the lack of documentation for this express.Router - I assume this is not the preferred way - but is it possible to create a solid RESTful backend with express.Router - and does it have all the basic HTTP verbs attached to it?
Another confusing thing is ----> in app.js we have an instance of router app.use(express.Router())
- how does this router instance interact with the others? Makes little sense on the face of it.
- Link to Express API Documentation: expressjs.com/api.html – aadarshsg Commented Jun 29, 2015 at 1:48
- 1 thanks but that's not really constructive. For example, one problem with express.Router is that app.params don't seem to work with it. And there is not really an express.params or router.params that I know of. – Alexander Mills Commented Jun 29, 2015 at 1:57
- according to this scotch.io/tutorials/learn-to-use-the-new-router-in-expressjs-4 there is a router.params functionality, but all in all it's not super clear how to use express.Router() multiple times or even once in an Express application – Alexander Mills Commented Jun 29, 2015 at 2:28
4 Answers
Reset to default 14As Bidhan A's answer states, this is preferred way to do it with Express and looks like that since Express 4.
You can completly modulate your code and logic.
For example you can have a routes/APIRouter.js
file with this code:
var apiRouter = express.Router();
apiRouter.get('/reports', controllers.getReports);
apiRouter.get('/reports/:id', controllers.getReport);
apiRouter.post('/reports', controllers.createReport);
apiRouter.put('/reports/:id', controllers.updateReport);
apiRouter.delete('/reports/:id', controllers.deleteReport);
Also you could have /controllers/reportsController.js
and finally at your main file app.js
or also named server.js
get:
var express = require('express');
var app = new express();
var apiRouter = require('./routes/APIRouter');
app.use('/api',apiRouter);
So, answering your question:
- Yes this is preferred and somehow official way to do it.
- Yes, you got whole HTTP to control by using
Router
and you should use another express based modules like:body-parser
,error-handler
orcookie-parser
in order to complete that control.
Note: this assumes you already know a preferred general web framework directory structure and do module exports.
I actually think that this is in fact the preferred way. You define your routes separately and simply use it in your app. It provides a nice separation of concerns. It also makes testing your routes quite easy. And yes, you can create a solid RESTful backend using express.Router. Also, it has all the basic HTTP verbs like get, post, put, delete, patch etc. attached to it.
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.)
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'))
本文标签: javascriptexpressRouter() vs appgetStack Overflow
版权声明:本文标题:javascript - express.Router() vs. app.get - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738421095a2085865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论