Permalink
Browse files

Refactor code

  • Loading branch information...
amio committed Mar 26, 2018
1 parent 51fba08 commit 91a18dac311cd098066f6e15bcb509db8602ae0b
Showing with 20 additions and 15 deletions.
  1. +1 −0 .gitignore
  2. +7 −7 README.md
  3. +11 −7 lib/index.js
  4. +1 −1 package.json
View
@@ -1,2 +1,3 @@
*.log
node_modules
config.json
View
@@ -46,19 +46,19 @@ All configurations are optional:
```javascript
{
"routes": {
// redirection (<value> is a url)
// redirect to a url
"home": "https://github.com/amio/now-go",
// proxy mode (<value> is a url prefixed with "PROXY >> ")
"ocelot.jpg": "PROXY >> https://example.com/path-to-ocelog.jpg",
// echo a string (<value> is a string)
// echo a string
"now-go": "Now go, let the legend come back to life!",
// proxy mode
"ocelot.jpg": "PROXY >> https://amio.github.io/now-go-configs/ocelot.jpg",
// special route for root path (<key> === "/")
// special route for __root path__
"/": "Yet another tinyurl service.",
// special route for unmatched path: (<key> === "?")
// special route for __unmatched path__
"?": "What are you looking for?"
},
"port": 3030 // optional, 3000 by default
"port": 8080 // optional, 3000 by default
}
```
View
@@ -59,6 +59,7 @@ function fetchLocalConfig (configPath) {
const userConfig = require(path.resolve(configPath))
resolve(deepAssign(DEFAULT_CONFIG, userConfig))
} catch (e) {
console.error(e.message)
resolve(deepAssign(DEFAULT_CONFIG, {
'routes': { '?': ERROR_MSG_PREFIX + e.message }
}))
@@ -72,23 +73,26 @@ function createRouter (routes) {
const signpost = routes[key] || routes['?'] || 'Oops'
const ok = !!routes[key]
// redirection
if (isURL(signpost)) {
// redirection
const statusCode = ok ? 301 : 302
res.writeHead(statusCode, { 'Location': signpost })
res.end()
} else if (isPROXY(signpost)) {
// proxy
return
}
// proxy
if (isPROXY(signpost)) {
const url = signpost.replace(PROXY_MODE_PREFIX, '')
return fetch(url).then(r => {
res.setHeader('Content-Type', r.headers.get('Content-Type'))
return r.body
})
} else {
// echo
const statusCode = ok ? 200 : 404
micro.send(res, statusCode, signpost)
}
// default: echo
const statusCode = ok ? 200 : 404
micro.send(res, statusCode, signpost)
}
}
View
@@ -5,7 +5,7 @@
"bin": "bin/now-go",
"main": "lib/index.js",
"scripts": {
"start": "bin/now-go",
"start": "bin/now-go -c https://amio.github.io/now-go-configs/go.json",
"test": "standard lib/**"
},
"dependencies": {

0 comments on commit 91a18da

Please sign in to comment.