我想从第一个中间件传递一些变量到另一个中间件,我试着这样做,但有“req。有些变量是一个给定的‘未定义’”。


//app.js
..
app.get('/someurl/', middleware1, middleware2)
...

////middleware1
...
some conditions
...
res.somevariable = variable1;
next();
...

////middleware2
...
some conditions
...
variable = req.somevariable;
...

在我问app.router之前,我想我至少应该解释一下我认为在使用中间件时会发生什么。要使用中间件,需要使用的函数是app.use()。当中间件正在执行时,它将使用next()调用下一个中间件,或者使它不再调用更多的中间件。这意味着我放置中间件调用的顺序很重要,因为一些中间件依赖于其他中间件,而靠近末尾的一些中间件甚至可能不被调用。

Today I was working on my application and had my server running in the background. I wanted to make some changes and refresh my page and see the changes immediately. Specifically, I was making changes to my layout. I couldn't get it to work so I searched Stack Overflow for the answer and found this question. It says to make sure that express.static() is beneath require('stylus'). But when I was looking at that OP's code, I saw that he had his app.router call at the very end of his middleware calls, and I tried to figure out why that was.

当我做我的express. js应用程序(3.0.0rc4版),我使用命令express app -sessions -css stylus和在我的app.js文件的代码来设置与我的app.router以上的express.static()和要求('stylus')调用。所以看起来,如果它已经这样设置了,那么它应该保持这样。

在重新安排我的代码,这样我就可以看到我的手写笔的变化,它看起来像这样:

app.configure(function(){
  //app.set() calls
  //app.use() calls
  //...
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(__dirname + '/public', {maxAge: 31557600000}));
});

app.get('/', routes.index);

app.get('/test', function(req, res){
  res.send('Test');
});

So I decided that the first step would be to find out why it is important to even have app.router in my code. So I commented it out, started my app and navigated to /. It displayed my index page just fine. Hmm, maybe it worked because I was exporting the routing from my routes file (routes.index). So next I navigated to /test and it displayed Test on the screen. Haha, OK, I have no idea what app.router does. Whether it is included in my code or not, my routing is fine. So I am definitely missing something.

所以我的问题是:

谁能解释一下app.router是做什么的,它的重要性,以及我应该把它放在中间件调用的哪里?如果我能得到一个关于express.static()的简要解释,那就太好了。据我所知,express.static()是我的信息的缓存,如果应用程序找不到所请求的页面,它将检查缓存,看看它是否存在。

尽管我很了解JavaScript,但我还是不明白Node.js生态系统中的这三个项目到底是做什么的。是不是有点像《Rails’Rack》?有人能解释一下吗?

我正在使用express 4.0,我知道主体解析器已经从express核心中取出,我正在使用推荐的替换,但我正在获得

bodyParser:使用单独的json/urlencoded中间件server.js:15:12 对扩展解析显式指定"extended: true" node_modules/ Body-parser /index.js:74:29

我在哪里可以找到这些所谓的中间件?或者我不应该得到这个错误?

var express     = require('express');
var server      = express();
var bodyParser  = require('body-parser');
var mongoose    = require('mongoose');
var passport    = require('./config/passport');
var routes      = require('./routes');

mongoose.connect('mongodb://localhost/myapp', function(err) {
    if(err) throw err;
});

server.set('view engine', 'jade');
server.set('views', __dirname + '/views');

server.use(bodyParser()); 
server.use(passport.initialize());

// Application Level Routes
routes(server, passport);

server.use(express.static(__dirname + '/public'));

server.listen(3000);