diff --git a/README.md b/README.md index 67b6cc6..f44866d 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,14 @@ Request body: { "text": "I felt happy because I saw the others were happy and because I knew I should feel happy"} Response body: [{"word": "happy", "count": 3}] -``` \ No newline at end of file +``` + +#### /comment-words +* `POST` : Returns the word count in the submitted comment + +``` +Request body: +{ "comment": "I find it ridiculous to limit me!"} +Response body: +{"count": 7} +``` diff --git a/server.js b/server.js index de0fea0..2773819 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,7 @@ const server = http.createServer((req, res) => { if (req.url === "/user-agent") return respondU(req, res); if (req.url.match(/^\/b64\//)) return respondB(req, res); if (req.url === '/repetitive-word') return respondRepetitiveWord(req, res) + if (req.url === '/comment-words') return respondCommentWord(req, res) res.end(); }); @@ -37,6 +38,18 @@ function respondRepetitiveWord (req, res) { }) } +function respondCommentWord (req, res) { + let body = '' + req.on('data', chunk => { + body += chunk.toString() + }) + req.on('end', () => { + const comment = JSON.parse(body).comment + res.setHeader('Content-Type', 'application/json') + res.end(JSON.stringify({ count: comment.split(' ').length })) + }) +} + server.listen(PORT); console.log(`Server listening on port ${PORT}`); diff --git a/test.js b/test.js index 2b0d3c5..add2fe0 100644 --- a/test.js +++ b/test.js @@ -41,13 +41,23 @@ tape('should respond repetitive-word', (t) => { } jsonist.post(`${urlBase}/repetitive-word`, data, (err, body) => { if (err) t.error(err) - t.equal(body[0].count, 4) t.equal(body[0].word, 'happy') t.end() }) }) +tape('should respond comment-words', (t) => { + const data = { + comment: 'If is specified, git rebase will perform an automatic git switch before doing anything else.' + } + jsonist.post(`${urlBase}/comment-words`, data, (err, body) => { + if (err) t.error(err) + t.equal(body.count, 17) + t.end() + }) +}) + tape("cleanup", function(t) { server.close(); t.end();