简体   繁体   English

base64 解码不重调文本 node.js

[英]base64 decoding not retuning text node.js

so i have some code所以我有一些代码

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    /* parse the url*/
    var q = url.parse(req.url, true).query;
    /*de encript it*/
    var base64 = Buffer.from(q.addr, "base64")
    /* JSON encoding*/
    console.log(base64)
    let data = {
        addr: q.addr ,
        base64: base64 ,
    };
    /*outputing*/
    var txt = JSON.stringify(data);;
    res.end(txt);
}).listen(8080);

when i go to http://localhost:8080/?addr=aGVsbG8gd29ybGQ= i expect to see "hello, world" in the console, and this webpage:当我从 go 到http://localhost:8080/?addr=aGVsbG8gd29ybGQ=我希望在控制台中看到“你好,世界”,这个网页:

{"addr":"aGVsbG8gd29ybGQ=","base64": "hello world"}

but insted i get但我明白了

<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64> <缓冲器68 65 6c 6c 6f 20 77 6f 72 6c 64>

and

{"addr":"aGVsbG8gd29ybGQ=","base64":{"type":"Buffer","data":[104,101,108,108,111,32,119,111,114,108,100]}}

i understand this is ASCII but how do i get it to display as text.我知道这是 ASCII 但我如何让它显示为文本。 (i want limaler function to the atob() feature in client js) (我想要 limaler function 到客户端 js 中的atob()功能)

You need to add toString method to the buffer:您需要将toString方法添加到缓冲区:

var base64 = Buffer.from(q.addr, "base64").toString();

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

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