Created
January 6, 2013 04:11
-
-
Save inadarei/4465153 to your computer and use it in GitHub Desktop.
base64 encode/decode in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = "http://cdn1.giltcdn.com/images/share/uploads/0000/0001/7250/172502872/420x560.jpg"; | |
var encoded = new Buffer(url).toString('base64'); | |
var decoded = new Buffer(encoded, 'base64').toString('ascii') | |
console.log(encoded); | |
console.log(decoded); |
@madanmishra i think you forgot to copy to line 2.
The line 3 is using the variable encoded
from the line 2. Then the line 3 can't work without the line 2.
Hope it helps.
The usage of new Buffer()
is deprecated. You could use instead new Buffer.from(url).toString('base64')
I know its probably a typo, but I want to clarify that it should be Buffer.from(url).toString('base64')
without the new
keyword.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting this error
var decoded = new Buffer(encoded, 'base64').toString('ascii')
^
ReferenceError: encoded is not defined
at Object. (C:\Users\madan\Desktop\myfinal\index.js:3:26)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
at bootstrap_node.js:538:3