Permalink
Browse files

Add tests

  • Loading branch information...
amio committed May 15, 2018
1 parent ed9fecc commit b5e9ca5a46ed7f9229dbdeecb002107c20a53c8b
Showing with 3,500 additions and 98 deletions.
  1. +1 −1 README.md
  2. +8 −8 index.js
  3. +3,459 −87 package-lock.json
  4. +5 −2 package.json
  5. +22 −0 test/handler.spec.js
  6. +5 −0 test/server.js
View
@@ -9,7 +9,7 @@ A tinyurl service.
## 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).
- Three types of routes:
View
@@ -1,19 +1,19 @@
const http = require('http')
function main (routesConfig, options = {}) {
const router = createRouter(routesConfig)
const router = createHandler(routesConfig)
const { port = 3000 } = options
http.createServer(router).listen(port)
}
function createRouter (routes) {
function createHandler (routes) {
return function router (req, res) {
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) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
return res.end('TOO_MUCH_DEPTH')
@@ -29,9 +29,9 @@ function signpostHandler (signpost, req, res, depth) {
return res.end(signpost)
}
if (isHandler(signpost)) {
if (isFunc(signpost)) {
try {
return signpostHandler(signpost(req), req, res, depth + 1)
return _signpostHandler(signpost(req), req, res, depth + 1)
} catch (e) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(`CONFIG ERROR: ${e.toString()}`)
@@ -55,9 +55,9 @@ function isText (text) {
return typeof text === 'string'
}
function isHandler (signpost) {
function isFunc (signpost) {
return typeof signpost === 'function'
}
module.exports = main
exports.createHandler = createRouter
module.exports.createHandler = createHandler
Oops, something went wrong.

0 comments on commit b5e9ca5

Please sign in to comment.