新手学习Nodejs和使用express遇到问题求助

新手学习Nodejs和使用express遇到问题求助

//app.js
var util = require(‘util’);
app.locals({
inspect:function(obj){
return util.inspect(obj, true);
}
});
app.use(function(req,res,next){
app.locals.headers =req.headers;
next();
});
/app.dynamicHelpers(
{
headers:function(req,res){
return req.headers;
}
}
);/
app.get(’/helper’,function(req,res){
res.render(‘helper’,{title:‘Helpers’});
});
//helpers.ejs
<%=inspect(headers)%>

初学NODE,按照教程上的做,把教程上的dynamicHelpers方法改了,现在遇到这样问题,当我直接访问/help这个地址时报错:headers is not defined 但是当我先访问/123 再访问/helper时就把header信息给打印出来了, 我希望能够第一次直接访问/helper 就能把header信息打印出来,不知道怎么做, 可以的话还希望能请帮忙解释下原因。谢谢


5 回复

当然可以!让我们来分析一下你所遇到的问题,并提供相应的解决方案。

问题分析

在你的代码中,你试图通过 app.locals 来定义一个全局的 inspect 函数,并且在中间件中将请求头(headers)赋值给 app.locals。然后你希望在 helper.ejs 模板中能够访问到这些信息。

但是,你遇到了一个问题:当你直接访问 /helper 路由时,headers 并没有被定义。然而,当你先访问另一个路由(例如 /123),然后再访问 /helper 时,headers 信息就成功地被打印出来了。

原因

这个问题的原因在于 app.locals 是在中间件中设置的,而中间件是在路由处理之前执行的。如果你直接访问 /helper 路由,那么在渲染模板之前,headers 还没有被设置到 app.locals 中。

解决方案

你可以通过确保在渲染模板之前,headers 已经被正确设置来解决这个问题。具体来说,可以在每次访问 /helper 路由时,重新设置 headers

以下是修改后的代码:

// app.js
var express = require('express');
var app = express();

app.locals.inspect = function(obj) {
    var util = require('util');
    return util.inspect(obj, true);
};

app.use(function(req, res, next) {
    // 在每次请求时设置 headers
    app.locals.headers = req.headers;
    next();
});

app.get('/helper', function(req, res) {
    // 渲染模板并传递 headers
    res.render('helper', { title: 'Helpers', headers: app.locals.headers });
});

// helpers.ejs
<%= inspect(headers) %>

解释

  1. 中间件设置

    • 在中间件中,我们每次都设置 app.locals.headers 为当前请求的头部信息。
  2. 路由处理

    • /helper 路由的处理函数中,我们将 headers 作为变量传递给模板,这样即使在第一次访问时,headers 也会被正确设置。
  3. 模板渲染

    • 在模板 helpers.ejs 中,使用 <%= inspect(headers) %> 来输出头部信息。

通过这种方式,无论何时访问 /helper 路由,headers 都会被正确设置并传递给模板进行渲染。这样你就不会再遇到 headers is not defined 的错误了。


同问,这个有解吗?

同问 如何使用中间件 + res.locals 替代dynamicHelpers

楼主用的express哪个版本? 3.x可以使用res.locals.headers解决的

根据你的描述,你希望在访问 /helper 路由时能够立即获取并显示 req.headers 信息。目前的问题是,app.locals 中的 headers 在初次访问时没有被正确设置。

问题分析

你当前的中间件 app.use() 确保了在每次请求时更新 app.locals.headers。然而,问题在于你希望在 /helper 路由中直接使用 headers 信息。由于 app.locals 是全局对象,它应该可以在任何地方访问到。

解决方案

你可以通过以下方式来解决这个问题:

  1. 确保 headers 在请求前已经被正确设置:使用中间件确保每次请求前都设置 app.locals.headers
  2. 在视图中使用 app.locals 中的 headers:在 EJS 模板中正确引用 headers

以下是修改后的代码:

app.js

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

// 设置中间件确保每次请求前都更新 headers
app.use(function(req, res, next) {
    app.locals.headers = req.headers;
    next();
});

app.set('view engine', 'ejs');

app.get('/helper', function(req, res) {
    res.render('helper', { title: 'Helpers' });
});

app.listen(3000, () => console.log('Server running on port 3000'));

helpers.ejs

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
</head>
<body>
    <h1>Headers</h1>
    <pre><%- inspect(app.locals.headers) %></pre>
</body>
</html>

关键点解释

  1. 中间件设置:在每次请求前使用 app.use() 中间件来更新 app.locals.headers,确保在渲染视图之前该信息已经准备好。
  2. EJS 模板:在模板中使用 <%- inspect(app.locals.headers) %> 来正确显示 headers 信息。

这样,在访问 /helper 路由时,应该可以直接看到 headers 信息而不会报错。

回到顶部