Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nas5w committed Feb 22, 2019
0 parents commit 42f8011
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
9 changes: 9 additions & 0 deletions app.js
@@ -0,0 +1,9 @@
const router = require('./src/diy-router');
const app = router();
const port = 3000;

//app.get('/', (req, res) => res.send('Hello World!'));

app
.listen(port)
.then(() => console.log(`Example app listening on port ${port}!`));
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
@@ -0,0 +1,14 @@
{
"name": "diy-node-router",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"route-parser": "0.0.5"
}
}
30 changes: 30 additions & 0 deletions src/diy-router.js
@@ -0,0 +1,30 @@
const http = require('http');
const Route = require('route-parser');

const routes = [];

const addRoute = (method, route, handler) => {
routes.push({ method, route, handler });
};

const get = (route, handler) => addRoute('get', route, handler);
const post = (route, handler) => addRoute('post', route, handler);

const router = () => {
const listen = async port => {
return http
.createServer(async (req, res) => {
res.write('Hello World!');
res.end();
})
.listen(port);
};

return {
get,
post,
listen
};
};

module.exports = router;

0 comments on commit 42f8011

Please sign in to comment.