简体   繁体   中英

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:

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

but insted i get

<Buffer 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. (i want limaler function to the atob() feature in client js)

You need to add toString method to the buffer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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