From 98ead1019d0c91162c92d69ae17e3921afbe2f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cspb-superstruct=E2=80=9D?= <“andrei@superstruct.tech”> Date: Sun, 15 Mar 2020 13:07:48 -0700 Subject: [PATCH] feat: add base64 endpoint --- server.js | 6 ++++++ test.js | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/server.js b/server.js index 1fe2a13..80dc423 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,7 @@ const PORT = process.env.PORT || 3000 const server = http.createServer((req, res) => { if (req.url === '/') return respondHello(req, res) + if (req.url.match(/^\/b64\//)) return respondBase64(req, res) res.end() }) @@ -12,6 +13,11 @@ function respondHello (req, res) { res.end(JSON.stringify({ msg: 'hello' })) } +function respondBase64 (req, res) { + const phrase = req.url.replace(/^\/b64\//, '') + res.end(JSON.stringify({ b64: Buffer.from(phrase).toString('base64') })) +} + server.listen(PORT) console.log(`Server listening on port ${PORT}`) diff --git a/test.js b/test.js index 3eb923d..fbdde4f 100644 --- a/test.js +++ b/test.js @@ -15,6 +15,15 @@ tape('should respond hello', (t) => { }) }) +tape('should respond b64', (t) => { + jsonist.get(`${urlBase}/b64/hello`, (err, body) => { + if (err) t.error(err) + + t.equal(body.b64, 'aGVsbG8=') + t.end() + }) +}) + tape('cleanup', function (t) { server.close() t.end()