将js编码标头表达为utf-8 [英] express js encode headers to utf-8
本文介绍了将js编码标头表达为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Express JS 4.13.3和Node.js 12
我收到带有标头name: "John D�m"
的请求,我什至不知道它的编码,所以我不知道如何转换它.
结果应该是name: "John Döm"
I am using express js 4.13.3 and nodejs 12
I am getting request with header name: "John D�m"
I don't know even the encoding it has, so I don't know how to convert it.
the result should be 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进行编码吗?与中间件之类的东西? 谢谢你.
can I set express to encode? with middleware or something? thank you in advanced.
推荐答案
出于某种原因,这对我有用:
For some reason this worked for me:
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>');
这篇关于将js编码标头表达为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文