简体   繁体   English

发送后无法设置标题

[英]Can't set headers after they're sent

I have an issue I've seen on here before, but I dont' know why it's happening in my "app". 我以前在这里遇到过一个问题,但是我不知道为什么它会在我的“应用程序”中发生。 I am getting the "can't set headers after they're sent" error whenever I just go to the route, / . 我收到“不能设置头后他们派”的错误时,我刚去的路线, / I'm sure I'm missing something basic. 我确定我缺少一些基本知识。 I've looked at the documentation and I've looked at other answers here, but I still can't seem to see why this would happen. 我看了文档,在这里看了其他答案,但是我仍然看不出为什么会发生这种情况。 Can someone please walk through why this error is occuring? 有人可以逐步解释为什么会发生此错误吗?

When I change writeHead to setHeader, everything works accordingly. 当我将writeHead更改为setHeader时,一切都会相应进行。 Where else would I have sent the headers other than / ? /之外,我还将标题发送到哪里?

// Basic Setup
var express = require('express')
, app = express()
, bodyParser = require('body-parser')
, port = process.env.PORT || 3000;

// Middleware
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

// Setup Routes
var router = express.Router();

router.get('/about', function(req, res){
  res.json({name: "tester"});
});

router.get('/', function(req, res){
  res.writeHead('200', {"Content-Type": 'text/html'});
  res.send("<html><head></head><body>Welcome to something. Sent from server.</body>        </html>");
});

app.use('/', router);

app.listen(port);
console.log("listening on port " + port);`

You are allowed to call res.setHeader(name, value) as often as you want until you call res.writeHead(statusCode) . 允许您res.setHeader(name, value)调用res.setHeader(name, value)直到调用res.writeHead(statusCode)为止。 After writeHead , the headers are baked in and you can only call res.write(data) , and finally res.end(data) . writeHead之后,头将被烘焙,您只能调用res.write(data) ,最后res.end(data)

Try this: 尝试这个:

Any exceptions within middleware function(req, res, next) (Connect/Express only)
res.send(body|status[, headers|status[, status]]) (Express only)

SetHeader : SetHeader

Sets a single header value for implicit headers. 为隐式标头设置单个标头值。 If this header already exists in the to-be-sent headers, it's value will be replaced. 如果此标头已存在于待发送标头中,则其值将被替换。 Use an array of strings here if you need to send multiple headers with the same name. 如果您需要发送多个具有相同名称的标头,请在此处使用字符串数组。

As Given Here: 如此处给出:

http://nodejs.org/docs/v0.4.0/api/http.html#response.setHeader http://nodejs.org/docs/v0.4.0/api/http.html#response.setHeader

WriteHead : WriteHead

Sends a response header to the request. 将响应标头发送到请求。 The status code is a 3-digit HTTP status code, like 404. The last argument, headers, are the response headers. 状态码是3位HTTP状态码,例如404。最后一个参数,标头,是响应标头。 Optionally one can give a human-readable reasonPhrase as the second argument. 可以选择提供人类可读的reasonPhrase作为第二个参数。

This method must only be called once on a message and it must be called before response.end() is called. 只能在消息上调用此方法一次,并且必须在调用response.end()之前调用此方法。

If you call response.write() or response.end() before calling this, the implicit/mutable headers will be calculated and call this function for you. 如果在调用此函数之前调用response.write()或response.end(),将计算隐式/可变头,并为您调用此函数。

As Given Here: 如此处给出:

http://nodejs.org/docs/v0.4.0/api/http.html#response.writeHead http://nodejs.org/docs/v0.4.0/api/http.html#response.writeHead

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

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