Deviopath › Node.js

Text to Base64

Easy · Buffers

In Node, Buffer.from(text).toString("base64") encodes the UTF-8 bytes of text as base64.

Write a function toBase64(text) that returns the same result. Buffer is not available here, so use TextEncoder to get the UTF-8 bytes and btoa to encode them.

btoa only accepts characters with codes 0-255 (btoa("€") throws), so first turn each byte into one character with String.fromCharCode.

Next: Parse the Query String