Nodejs编码问题
Nodejs encoding issue
我正在尝试从请求中获取数据,但格式或编码不是我要找的。
我尝试使用 req.setEncoding('utf8')
设置编码
我应该得到的字符串是:
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (..)
main : Element
main = collage 500 500 [filled orange (circle (1 + 49)]
我实际得到的是:import+Graphics.Element+exposing+%28..%29%0D%0Aimport+Graphics.Collage+exposing+%28..%29%0D%0Aimport+Color+exposing+%28..%29%0D%0Amain+%3A+Element%0D%0Amain+%3D+collage+500+500+%5Bfilled+orange+%28circle+%281+%2B+49%29%5D
这是我读取数据并设置编码的地方:
function onPost () {
// When there is a POST request
app.post('/elmsim.html',function (req, res) {
console.log('POST request received from elmsim')
req.setEncoding('ascii')
req.on('data', function (data) {
// Create new directory
createDir(data, res)
})
})
}
任何帮助都会很棒!谢谢
您得到的字符串是 url 编码的字符串。
您尝试过对字符串调用 decodeUriComponent 吗?
decodeURIComponent( string )
Luca 的答案是正确的,但 decodeURIComponent 不适用于包含加号的字符串。您必须使用“%2B”作为分隔符(这代表一个加号)来拆分字符串,并将 decodeURIComponent 应用于每个单独的字符串。然后可以连接字符串,并可以添加加号。
这是我的解决方案:
function decodeWithPlus(str) {
// Create array seperated by +
var splittedstr = str.split('%2B')
// Decode each array element and add to output string seperated by '+'
var outs = ''
var first = true
splittedstr.forEach(function (element) {
if (first) {
outs += replaceAll('+', ' ', decodeURIComponent(element))
first = false
}
else {
outs += '+' + replaceAll('+', ' ', decodeURIComponent(element))
}
})
return outs
}
function replaceAll(find, replace, str) {
var outs = ''
for (i = 0; i < str.length; i++) {
if (str[i] === find) {
outs += replace
}
else {
outs += str[i]
}
}
return outs
}
我正在尝试从请求中获取数据,但格式或编码不是我要找的。
我尝试使用 req.setEncoding('utf8')
我应该得到的字符串是:
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Color exposing (..)
main : Element
main = collage 500 500 [filled orange (circle (1 + 49)]
我实际得到的是:import+Graphics.Element+exposing+%28..%29%0D%0Aimport+Graphics.Collage+exposing+%28..%29%0D%0Aimport+Color+exposing+%28..%29%0D%0Amain+%3A+Element%0D%0Amain+%3D+collage+500+500+%5Bfilled+orange+%28circle+%281+%2B+49%29%5D
这是我读取数据并设置编码的地方:
function onPost () {
// When there is a POST request
app.post('/elmsim.html',function (req, res) {
console.log('POST request received from elmsim')
req.setEncoding('ascii')
req.on('data', function (data) {
// Create new directory
createDir(data, res)
})
})
}
任何帮助都会很棒!谢谢
您得到的字符串是 url 编码的字符串。
您尝试过对字符串调用 decodeUriComponent 吗?
decodeURIComponent( string )
Luca 的答案是正确的,但 decodeURIComponent 不适用于包含加号的字符串。您必须使用“%2B”作为分隔符(这代表一个加号)来拆分字符串,并将 decodeURIComponent 应用于每个单独的字符串。然后可以连接字符串,并可以添加加号。
这是我的解决方案:
function decodeWithPlus(str) {
// Create array seperated by +
var splittedstr = str.split('%2B')
// Decode each array element and add to output string seperated by '+'
var outs = ''
var first = true
splittedstr.forEach(function (element) {
if (first) {
outs += replaceAll('+', ' ', decodeURIComponent(element))
first = false
}
else {
outs += '+' + replaceAll('+', ' ', decodeURIComponent(element))
}
})
return outs
}
function replaceAll(find, replace, str) {
var outs = ''
for (i = 0; i < str.length; i++) {
if (str[i] === find) {
outs += replace
}
else {
outs += str[i]
}
}
return outs
}