res.json 没有 headers 的 http 状态代码
res.json doesn't have headers for http status code
我希望 headers 陪同我发送的 json object。当我尝试 setHeaders
时,出现 Can't set headers after they are sent.
错误。
我在这个 SO post 中看到
Since Express.js 3x the response object has a json() method which sets
all the headers correctly for you and returns the response in JSON
format.
但是,当我执行 res.status(404).json({"error", "message not found"})
时,没有 headers(http 状态)伴随发送给客户端的 json object。所有客户看到的是:
{
"error:": "Message Not Found"
}
网络服务:
var express = require('express');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var app = express();
var message_dict = [];
app.set('json spaces', 40);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.json());
app.post('/messages', function(request, response){
var string_message = request.body.message;
var json_obj = request.body
var hash_key = crypto.createHash('sha256').update(string_message).digest('hex');
message_dict[hash_key] = json_obj
var encrypted_data = {'digest' : hash_key};
response.json(encrypted_data);
});
app.get('/messages/*', function(request, response) {
var hash_key = request.params[0];
if(hash_key in message_dict){
response.json(message_dict[hash_key]);
}
else{
response.status(404).json({'error:' : 'Messag Not Found'})
}
});
app.listen(8080, function() {
console.log('Example app listening on port 8080!');
});
我认为您混淆了响应 headers 和响应 body,.json()
设置了正确的响应 headers(特别是 Content-Type
),响应body 仍然只是您传递给调用的参数。
您是否有特定原因希望响应 headers 出现在响应 body 中? response headers 通常对请求者用处不大,在从服务器 (res.headers
属性) 获得响应时可以轻松访问。
实际回答您的问题(不确定您为什么要这样做):
var json = {'error:' : 'Messag Not Found'}
response.type('application/json')
response.writeHead(404)
json.headers = response._header; // _header not documented but works
response.end(JSON.stringify(json));
首先,由于您现在 post 提出这个问题,我假设您使用的是 Express 4.x 而不是 3.x,这是其中引用的 Express 版本所以 post 你链接到了,所以不要听从几年前 post 的建议。很多功能的操作从版本 3 到版本 4 发生了变化,所以很多关于特定功能行为的答案可能是 out-of-date。可以找到 Express 4 文档 here.
我认为您可能混淆了 app.set()
函数和 res.set()
函数。 app.set()
函数为您的应用程序设置应用程序设置,而不是响应 object 的 header 值。有关如何使用 app.set()
函数的更多信息,请参阅 here。
如果要在响应 object 上设置单独的 header 值,则需要使用 res.set()
函数。 res.set()
函数的文档可以在 here 中找到。您可以使用此函数设置单个或多个 header 值,具体取决于您传递给该函数的参数。您可以在您的个人路由定义的回调函数中调用此函数。
为了解决您上面的初始问题,res.status()
function simply sets the res.statusCode
property on the response object to whatever value you specify, while the res.json()
function 将响应 object 的 body 设置为 JSON object 你指定的。简而言之,none 这些或任何其他 Express 函数会自动将值添加到响应 object 而无需您明确告诉它这样做。
我希望 headers 陪同我发送的 json object。当我尝试 setHeaders
时,出现 Can't set headers after they are sent.
错误。
我在这个 SO post 中看到
Since Express.js 3x the response object has a json() method which sets all the headers correctly for you and returns the response in JSON format.
但是,当我执行 res.status(404).json({"error", "message not found"})
时,没有 headers(http 状态)伴随发送给客户端的 json object。所有客户看到的是:
{
"error:": "Message Not Found"
}
网络服务:
var express = require('express');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var app = express();
var message_dict = [];
app.set('json spaces', 40);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.json());
app.post('/messages', function(request, response){
var string_message = request.body.message;
var json_obj = request.body
var hash_key = crypto.createHash('sha256').update(string_message).digest('hex');
message_dict[hash_key] = json_obj
var encrypted_data = {'digest' : hash_key};
response.json(encrypted_data);
});
app.get('/messages/*', function(request, response) {
var hash_key = request.params[0];
if(hash_key in message_dict){
response.json(message_dict[hash_key]);
}
else{
response.status(404).json({'error:' : 'Messag Not Found'})
}
});
app.listen(8080, function() {
console.log('Example app listening on port 8080!');
});
我认为您混淆了响应 headers 和响应 body,.json()
设置了正确的响应 headers(特别是 Content-Type
),响应body 仍然只是您传递给调用的参数。
您是否有特定原因希望响应 headers 出现在响应 body 中? response headers 通常对请求者用处不大,在从服务器 (res.headers
属性) 获得响应时可以轻松访问。
实际回答您的问题(不确定您为什么要这样做):
var json = {'error:' : 'Messag Not Found'}
response.type('application/json')
response.writeHead(404)
json.headers = response._header; // _header not documented but works
response.end(JSON.stringify(json));
首先,由于您现在 post 提出这个问题,我假设您使用的是 Express 4.x 而不是 3.x,这是其中引用的 Express 版本所以 post 你链接到了,所以不要听从几年前 post 的建议。很多功能的操作从版本 3 到版本 4 发生了变化,所以很多关于特定功能行为的答案可能是 out-of-date。可以找到 Express 4 文档 here.
我认为您可能混淆了 app.set()
函数和 res.set()
函数。 app.set()
函数为您的应用程序设置应用程序设置,而不是响应 object 的 header 值。有关如何使用 app.set()
函数的更多信息,请参阅 here。
如果要在响应 object 上设置单独的 header 值,则需要使用 res.set()
函数。 res.set()
函数的文档可以在 here 中找到。您可以使用此函数设置单个或多个 header 值,具体取决于您传递给该函数的参数。您可以在您的个人路由定义的回调函数中调用此函数。
为了解决您上面的初始问题,res.status()
function simply sets the res.statusCode
property on the response object to whatever value you specify, while the res.json()
function 将响应 object 的 body 设置为 JSON object 你指定的。简而言之,none 这些或任何其他 Express 函数会自动将值添加到响应 object 而无需您明确告诉它这样做。