简体   繁体   English

Express应用程序使用刷新行为

[英]Express app.use refresh behavior

I'm using Express with Node.js and am quite confused about refreshing behavior. 我将Express与Node.js结合使用,并对刷新行为感到困惑。 Upon refresh of /test , it seems like something is cached server-side when it hits app.use because the array length is nonzero (see sample code below). 刷新/test ,当它app.use时,似乎在服务器端缓存了app.use因为数组长度非零(请参见下面的示例代码)。 I would expect the array length to reset to zero since I'm hitting /test again when I'm refreshing the browser. 我希望数组长度会重置为零,因为刷新浏览器时再次点击/test

Does app.use cache things by default? app.use是否默认情况下缓存内容? How does Express middleware work in terms of refresh? Express中间件在刷新方面如何工作? I couldn't find anything that explained this clearly in the Express 4.14 docs. 我在Express 4.14文档中找不到任何能清楚解释这一点的内容。

================== ==================

Browser Endpoint: localhost:8000/test 浏览器端点: localhost:8000/test

Client: 客户:

$.get('/data').done(function(response){...}

Route: 路线:

module.exports = function(app) {
  app.get('/test', function(req,res) {
    var arr =[];
    app.use('/data', function(req,res, next) {
      console.log(arr.length); // this is nonzero on refresh
      arr.push(/* some stuff*/);
      res.send({"arr": arr});
    }
   res.render('test.html')
  }
}

Server: 服务器:

var express = require('express');
var app = express();

require('./routes/route')(app);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

var server = app.listen(8000, function() {
    console.log("server started 8000");
});
app.use(express.static(__dirname + '/public'));

It's not really server caching. 这不是真正的服务器缓存。 It's because you are registering middleware inside a closure and thus those closure variables (like arr ) are retained for the next invocation of the middleware. 这是因为您要在闭包内部注册中间件,因此这些闭包变量(如arr )将保留用于下一次调用中间件。 In addition, you're registering the middleware over and over (a new one each time /test is hit). 另外,您要一遍又一遍地注册中间件(每次/test命中一个新的中间件)。

When you register app.use() inside an app.get() that means that every time you hit the /test route, you will add another duplicate app.use() handler. 当您注册app.use()app.get()这意味着,每一次你打/test路线,你会再添重复app.use()处理程序。 They will accumulate over time and the same middleware will get run multiple times for for the same request, retaining the previous value for arr from when it was originally registered, but each with their own value for that array. 它们将随着时间的推移而累积,并且相同的中间件将针对相同的请求运行多次,并保留最初注册arr时的先前值,但每个值均具有该数组的自己的值。

The general solution here is to NOT register app.use() inside of app.get() because you only want one handler - you don't want them to accumulate. 这里的一般解决方法是不注册app.use()app.get()因为你只需要一个处理程序-你不希望他们积累。


It's unclear what you're trying to accomplish with your app.use('/data/, ...) middleware. 尚不清楚您要使用app.use('/data/, ...)中间件来完成什么工作。 It is clear that your current structure is wrong, but without understanding what you were trying to do with that, it's not clear exactly how it should be written. 很明显,您当前的结构是错误的,但是在不了解您要如何处理的情况下,尚不清楚应如何编写。 The usual function of middleware is to be registered once during the initialization of the server and never inside a request handler. 中间件的通常功能是在服务器初始化期间进行一次注册,而不是在请求处理程序中进行注册。


If you're trying to respond to your ajax call: 如果您想回应您的Ajax呼叫:

$.get('/data').done(function(response){...}

Then, you would want an app.get('/data', ...) at the top level of your app module to make that work. 然后,您需要在app模块的顶层使用app.get('/data', ...)来完成该工作。


Please explain what the arr.push() is supposed to accomplish for us to help in any more detail. 请解释arr.push()应该完成什么,以便我们提供更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM