Encoding/Decoding Base64 with Node.js Core Buffer API

The Node.js core Buffer API allows base64 encoding for cases like Basic authentication.

The legacy version with Node.js is by using the buf.atob() and buf.btoa() methods, and according to their documentation should not be used in new code.

So here’s…


This content originally appeared on DEV Community and was authored by Colby Hemond

The Node.js core Buffer API allows base64 encoding for cases like Basic authentication.

The legacy version with Node.js is by using the buf.atob() and buf.btoa() methods, and according to their documentation should not be used in new code.

So here's the recommended way...

Encoding a string to base64

const user = 'colbyhemond'
const password = 'test123!@#'
const stringBuffer = Buffer.from(`${user}:${password}`)
const stringBase64 = stringBuffer.toString('base64')

console.log(stringBase64)
// will output: "Y29sYnloZW1vbmQ6dGVzdDEyMyFAIw=="

Decoding from base64 to a unicode string

const base64String = 'Y29sYnloZW1vbmQ6dGVzdDEyMyFAIw=='
const base64Buffer = Buffer.from(base64String, 'base64')
const string =  base64Buffer.toString()

console.log(string)
// will output: "colbyhemond:test123!@#"

To see how you can turn this into your own encoding utility and publish it on NPM, checkout the post on my website.


This content originally appeared on DEV Community and was authored by Colby Hemond


Print Share Comment Cite Upload Translate Updates
APA

Colby Hemond | Sciencx (2022-06-15T20:47:49+00:00) Encoding/Decoding Base64 with Node.js Core Buffer API. Retrieved from https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/

MLA
" » Encoding/Decoding Base64 with Node.js Core Buffer API." Colby Hemond | Sciencx - Wednesday June 15, 2022, https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/
HARVARD
Colby Hemond | Sciencx Wednesday June 15, 2022 » Encoding/Decoding Base64 with Node.js Core Buffer API., viewed ,<https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/>
VANCOUVER
Colby Hemond | Sciencx - » Encoding/Decoding Base64 with Node.js Core Buffer API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/
CHICAGO
" » Encoding/Decoding Base64 with Node.js Core Buffer API." Colby Hemond | Sciencx - Accessed . https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/
IEEE
" » Encoding/Decoding Base64 with Node.js Core Buffer API." Colby Hemond | Sciencx [Online]. Available: https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/. [Accessed: ]
rf:citation
» Encoding/Decoding Base64 with Node.js Core Buffer API | Colby Hemond | Sciencx | https://www.scien.cx/2022/06/15/encoding-decoding-base64-with-node-js-core-buffer-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.