Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amio committed May 15, 2018
1 parent ed9fecc commit b5e9ca5
Show file tree
Hide file tree
Showing 6 changed files with 3,500 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A tinyurl service.


## Features ## Features


> NOTE: this is the README of v2. For v1 docs, check npm [current release][npm-link]. > WARNING: This is the doc of `now-go@next`. For v1 docs, check [current release][npm-link] on npm.
- Lightweight tinyurl service (in 100 lines). - Lightweight tinyurl service (in 100 lines).
- Three types of routes: - Three types of routes:
Expand Down
16 changes: 8 additions & 8 deletions index.js
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,19 @@
const http = require('http') const http = require('http')


function main (routesConfig, options = {}) { function main (routesConfig, options = {}) {
const router = createRouter(routesConfig) const router = createHandler(routesConfig)
const { port = 3000 } = options const { port = 3000 } = options
http.createServer(router).listen(port) http.createServer(router).listen(port)
} }


function createRouter (routes) { function createHandler (routes) {
return function router (req, res) { return function router (req, res) {
const signpost = routes[req.url] || routes['*'] const signpost = routes[req.url] || routes['*']
signpostHandler(signpost, req, res, 0) _signpostHandler(signpost, req, res, 0)
} }
} }


function signpostHandler (signpost, req, res, depth) { function _signpostHandler (signpost, req, res, depth) {
if (depth > 10) { if (depth > 10) {
res.writeHead(500, { 'Content-Type': 'text/plain' }) res.writeHead(500, { 'Content-Type': 'text/plain' })
return res.end('TOO_MUCH_DEPTH') return res.end('TOO_MUCH_DEPTH')
Expand All @@ -29,9 +29,9 @@ function signpostHandler (signpost, req, res, depth) {
return res.end(signpost) return res.end(signpost)
} }


if (isHandler(signpost)) { if (isFunc(signpost)) {
try { try {
return signpostHandler(signpost(req), req, res, depth + 1) return _signpostHandler(signpost(req), req, res, depth + 1)
} catch (e) { } catch (e) {
res.writeHead(500, { 'Content-Type': 'text/plain' }) res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(`CONFIG ERROR: ${e.toString()}`) res.end(`CONFIG ERROR: ${e.toString()}`)
Expand All @@ -55,9 +55,9 @@ function isText (text) {
return typeof text === 'string' return typeof text === 'string'
} }


function isHandler (signpost) { function isFunc (signpost) {
return typeof signpost === 'function' return typeof signpost === 'function'
} }


module.exports = main module.exports = main
exports.createHandler = createRouter module.exports.createHandler = createHandler
Loading

0 comments on commit b5e9ca5

Please sign in to comment.