Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const http = require("http");
const http = require('http')
const _ = require('lodash')

const PORT = process.env.PORT || 3000;
const PORT = process.env.PORT || 3000

const server = http.createServer((req, res) => {
if (req.url === "/") return respondHello(req, res);
if (req.url === "/user-agent") return respondU(req, res);
if (req.url.match(/^\/b64\//)) return respondB(req, res);
if (req.url === '/') return respondHello(req, res)
if (req.url === '/user-agent') return respondUserAgent(req, res)
if (req.url.match(/^\/b64\//)) return respondBase64(req, res)
Comment on lines -8 to +9
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactoring should be in a separate PR.

if (req.url === '/repetitive-word') return respondRepetitiveWord(req, res)

res.end();
});
res.end()
})

function respondHello(req, res) {
res.end(JSON.stringify({ msg: "hello" }));
function respondHello (req, res) {
res.end(JSON.stringify({ msg: 'hello' }))
}

function respondU(req, res) {
const ua = req.headers["user-agent"];
res.end(JSON.stringify({ ua }));
function respondUserAgent (req, res) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should include only code that contributes towards the style changes. You mixed style changes and refactoring (functions' names changes).

const ua = req.headers['user-agent']
res.end(JSON.stringify({ ua }))
}

function respondB(req, res) {
res.end(JSON.stringify({b64: Buffer.from(req.url.replace(/^\/b64\//, "")).toString("base64")}));
function respondBase64 (req, res) {
const base64 = req.url.replace(/^\/b64\//, '')
res.end(
JSON.stringify({
b64: Buffer.from(base64).toString('base64') }))
}

function respondRepetitiveWord (req, res) {
Expand All @@ -37,10 +40,10 @@ function respondRepetitiveWord (req, res) {
})
}

server.listen(PORT);
console.log(`Server listening on port ${PORT}`);
server.listen(PORT)
console.log(`Server listening on port ${PORT}`)

if (require.main !== module) module.exports = server;
if (require.main !== module) module.exports = server

function countWords (text) {
const parsedText = {}
Expand All @@ -63,4 +66,4 @@ function countWords (text) {
repetitiveWords.push(word)
})
return _.orderBy(repetitiveWords, ['count'], ['desc'])
}
}
56 changes: 28 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
const tape = require("tape");
const jsonist = require("jsonist");
const tape = require('tape')
const jsonist = require('jsonist')

const PORT = (process.env.PORT =
process.env.PORT || require("get-PORT-sync")());
const server = require("./server");
process.env.PORT || require('get-PORT-sync')())
const server = require('./server')

const urlBase = `http://localhost:${PORT}`;
const urlBase = `http://localhost:${PORT}`

tape("should respond hello", t => {
tape('should respond hello', t => {
jsonist.get(urlBase, (err, body) => {
if (err) t.error(err);
if (err) t.error(err)

t.equal(body.msg, "hello");
t.end();
});
});
t.equal(body.msg, 'hello')
t.end()
})
})

tape("should respond user-agent", t => {
const opts = { headers: { "User-Agent": "tape" } };
tape('should respond user-agent', t => {
const opts = { headers: { 'User-Agent': 'tape' } }
jsonist.get(`${urlBase}/user-agent`, opts, (err, body) => {
if (err) t.error(err);
if (err) t.error(err)

t.equal(body.ua, "tape");
t.end();
});
});
t.equal(body.ua, 'tape')
t.end()
})
})

tape("should respond b64", t => {
tape('should respond b64', t => {
jsonist.get(`${urlBase}/b64/hello`, (err, body) => {
if (err) t.error(err);
if (err) t.error(err)

t.equal(body.b64, "aGVsbG8=");
t.end();
});
});
t.equal(body.b64, 'aGVsbG8=')
t.end()
})
})

tape('should respond repetitive-word', (t) => {
const data = {
Expand All @@ -48,7 +48,7 @@ tape('should respond repetitive-word', (t) => {
})
})

tape("cleanup", function(t) {
server.close();
t.end();
});
tape('cleanup', function (t) {
server.close()
t.end()
})