NodeJS/Express应用程序的使用顺序和用法 [英] NodeJS/Express app.use sequence and usage
问题描述
我试图将节点路由分为两个部分:HTML/App和REST.这是我所做的:
I tried to separate my node routing into two parts: HTML/App and REST. Here is what I've done:
app.js:
var appPort = process.env.PORT || 80;
var express = require('express');
var http = require('http');
var appRouter = require('./routes/index');
var restRouter = require('./routes/rest');
var app = express();
var srv = http.createServer(app);
app.set('port', appPort);
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api/rest/', restRouter); // this seems not working .. I never get the expected response
app.use('/', appRouter); // I get this even with localhost/api/rest/...
var server = srv.listen(app.get('port'), function() {
debug('Express server listening ' + server.address().address + ':' + server.address().port);
});
index.js:
var express = require('express');
var router = express.Router();
router.get('/*', function (req, res) {
res.send('HOME')
});
module.exports = router;
rest.js
var express = require('express');
var router = express.Router();
router.get('/api/rest/*', function(req, res) {
res.send('REST API');
});
module.exports = router;
我的问题:
1.通常有可能以这种方式构建多个路由器吗?
2. get.use的顺序是否重要,和/或我是否必须处理"next"?
3.如果我想访问路由器内部的数据库,我可以交出这样的一个参数:
My questions:
1. It's possible in general to build multiple routers in this way?
2. Does the sequence of get.use matter, and/or do I have to deal with 'next'?
3. In case I would like to access a database inside the router can I hand over a parameter like this:
// ...
var client = new pg.Client(dbConnection);
// ...
app.use('/', appRouter(client));
推荐答案
1)可以通过这种方式构建多个路由器.
因为您正在使用此
app.use('/api/rest/', restRouter);
您在 rest.js
中的路由调用将相对于/api/rest/
,这意味着您的代码应在 rest.js
看起来像这样:
your route calls in rest.js
will be relative to /api/rest/
which means your code should be modified in rest.js
to look like this:
router.get('*', function(req, res) {
res.send('REST API');
});
我也鼓励您查看快速多路由器示例在GitHub上.通过显示具有版本化路由的REST应用程序,可以非常清楚地说明这一点.
I would also encourage you to see the Express multi-router example on GitHub. It illustrates this point very clearly by showing a REST app with versioned routes.
2)事物的顺序很重要
请参见有关app.use的快速文档,您会注意到:
中间件功能是顺序执行的,因此顺序中间件的包含很重要.
Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.
如果您颠倒了 app.use
调用的顺序,则 router.get('/*',function(req,res){
> index.js 会在您到达其他路线之前捕获所有内容……违背了您的目标.
If you reverse the order of your app.use
calls, the router.get('/*', function (req, res) {
line in index.js
will catch everything before you get to other routes...defeating your purpose.
此外,如果您不拨打 next
,Express将无法知道您已完成操作,甚至无法继续下一个中间件或路由.
Also, if you don't call next
, Express has no way to know that you are done or even that you want to continue to the next middleware or route.
3)数据库问题是模块/范围问题
这不是快速问题,更多的是范围问题.我建议您查找有关javascript范围以及 Node处理模块的方式的一些出色著作.
This is more of a scope question than an Express question. I'd suggest looking up some of the excellent writing about javascript scope and also on how Node handles modules.
这篇关于NodeJS/Express应用程序的使用顺序和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!