route-scout
Advanced tools
Comparing version
123
index.js
@@ -1,119 +0,10 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const url = require('url'); | ||
#!/usr/bin/env node | ||
class Router { | ||
constructor () { | ||
this.methods = { | ||
GET: [], | ||
POST: [], | ||
PATCH: [], | ||
PUT: [], | ||
DELETE: [] | ||
}; | ||
} | ||
createRoute(obj) { | ||
if (!this.methods[obj.method]) { | ||
this.methods[obj.method] = []; | ||
} | ||
this.methods[obj.method].push({ | ||
url: obj.url, | ||
handler: obj.handler | ||
}); | ||
return this; | ||
} | ||
get(url, handler) { | ||
this.methods.GET.push({url, handler}); | ||
return this; | ||
} | ||
post(url, handler) { | ||
this.methods.POST.push({url, handler}); | ||
return this; | ||
} | ||
patch(url, handler) { | ||
this.methods.PATCH.push({url, handler}); | ||
return this; | ||
} | ||
put(url, handler) { | ||
this.methods.PUT.push({url, handler}); | ||
return this; | ||
} | ||
delete(url, handler) { | ||
this.methods.DELETE.push({url, handler}); | ||
return this; | ||
} | ||
static(url) { | ||
this.methods.GET.push({url, handler: staticHandler}); | ||
return this; | ||
} | ||
routes() { | ||
return (req, res) => { | ||
const routes = this.methods[req.method]; | ||
if (routes) { | ||
const route = routes.find(r => { | ||
if (/:.[a-z0-9]+/gi.test(r.url)) { | ||
let reqUrl = req.url.split('/'); | ||
let routeUrl = r.url.split('/'); | ||
const server = require('./lib/server'); | ||
const port = process.argv[2] || 8080; | ||
if (reqUrl.length === routeUrl.length && reqUrl[1] === routeUrl[1]) { | ||
req.params = {}; | ||
server.listen(port, (err) => { | ||
if (err) console.error('Error message: %j', err); | ||
for (let i = 0, l = routeUrl.length; i < l; i++) { | ||
if (routeUrl[i][0] === ':') { | ||
let paramKey = routeUrl[i].replace(':', ''); | ||
req.params[paramKey] = reqUrl[i]; | ||
} | ||
} | ||
return 1; | ||
} | ||
return 0; | ||
} else { | ||
return req.url.indexOf(r.url) === 0; | ||
} | ||
}); | ||
/* If there is both a /test and a /testing route defined, /testing will return the /test handler. | ||
*/ | ||
if (route) { | ||
route.handler(req, res); | ||
return 0; | ||
} | ||
} | ||
res.writeHead(404); | ||
res.write('Not found.'); | ||
res.end(); | ||
return 1; | ||
}; | ||
} | ||
} | ||
const staticHandler = (req, res) => { | ||
const uri = url.parse(req.url).pathname; | ||
let filename = path.join(process.cwd(), uri); | ||
fs.stat(filename, function(err) { | ||
if(err) { | ||
res.writeHead(404, {'Content-Type': 'text/plain'}); | ||
res.write(`404 Not Found\n`); | ||
res.end(); | ||
return; | ||
} | ||
if (fs.statSync(filename).isDirectory()) filename += '/index.html'; | ||
fs.readFile(filename, 'binary', function(err, file) { | ||
if(err) { | ||
res.writeHead(500, {'Content-Type': 'text/plain'}); | ||
res.write(err + '\n'); | ||
res.end(); | ||
return; | ||
} | ||
res.writeHead(200); | ||
res.write(file, 'binary'); | ||
res.end(); | ||
}); | ||
}); | ||
}; | ||
module.exports = new Router(); | ||
console.log('Opened server on %j', server.address()); | ||
}); |
{ | ||
"name": "route-scout", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A framework that handles the routing for you and makes your server-creating life much easier. Also makes you look younger and more attractive. Results may vary.", | ||
"main": "index.js", | ||
"main": "lib/router.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "gulp test", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
43886
1020.11%18
800%417
282.57%1
-50%140
Infinity%2
100%