我正在使用express js 4.13.3和nodejs 12
我收到带有 header name: "John D�m"
的请求,我什至不知道它的编码,所以我不知道如何转换它。
结果应该是 name: "John Döm"
这是路由
var express = require('express')
var app = express();
app.configure(function () {
app.use(express.logger('dev'));
app.use(express.bodyParser());
});
app.get('/drivers', function(req,res){
res.send(req.headers["name"])
});
app.listen(3000);
console.log('Up: http://127.0.0.1:3000/');
我尝试了 res.header("Content-Type", "application/json; charset=utf-8");
我可以设置express编码吗?用中间件什么的? 提前谢谢您。
最佳答案
出于某种原因,这对我有用:
res.end(JSON.stringify(req.headers.name, null, 2), 'utf-8');
但不是这个:
res.send(req.headers.name)
所以我最终得到:
var string = req.headers.name;
return res.send('<!DOCTYPE html><html> <head><meta charset="utf-8"></head><body>' +
string +
'</body></html>');
关于node.js - Express js 将 header 编码为 utf-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35631575/