Skip to content

Commit

Permalink
Homework pirple#1
Browse files Browse the repository at this point in the history
Signed-off-by: Skye.Wu <[email protected]>
  • Loading branch information
Skye.Wu committed Oct 29, 2018
1 parent 728dd39 commit f9a388f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Homework Assignment #1/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* This is node environment configuration file
*
*/

// environment
const environment = {};

// normal environment
environment.normal = {
httpPort: 8000,
};

// environment to export
const environmentToExport = environment[process.env.NODE_ENV] || environment.normal;

// export
module.exports = environmentToExport;
34 changes: 34 additions & 0 deletions Homework Assignment #1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Homework Assignment #1
This is the first of several homework assignments you'll receive in this course. In order to receive your certificate of completion (at the end of this course) you must complete all the assignments and receive a passing grade.
How to Turn It In:
1. Create a public github repo for this assignment.
2. Create a new post in the Facebook Group and note "Homework Assignment #1" at the top.
3. In that thread, discuss what you have built, and include the link to your Github repo.
The Assignment:
Please create a simple "Hello World" API. Meaning:
1. It should be a RESTful JSON API that listens on a port of your choice.
2. When someone posts anything to the route /hello, you should return a welcome message, in JSON format. This message can be anything you want.
*/

// import library
const http = require('http');
const routes = require('./routes');
const config = require('./config');

// create http server
const httpServer = http.createServer(routes);

// listen http port
httpServer.listen(config.httpPort, () => {
console.log(`HTTP Server is running on port ${config.httpPort} now.`);
});
63 changes: 63 additions & 0 deletions Homework Assignment #1/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* This is route handler
*
*/

// request pathname parse
const getPathname = (url) => {
return url.replace(/^\/|\/$/g, '').trim();
};

// welcome message
const welcome = (req) => {
const pathname = getPathname(req.url);
const method = req.method.toUpperCase();
return `[${method}] /${pathname} Welcome!`;
};

// common method
const methods = {
get: (req, res) => {
res.end(welcome(req));
},
post: (req, res) => {
res.end(welcome(req));
},
put: (req, res) => {
res.end(welcome(req));
},
delete: (req, res) => {
res.end(welcome(req));
},
};

// routes mapping rules
const routeRules = {};

// route hello
routeRules.hello = Object.assign({}, methods);

// route not found
routeRules.notFound = (req, res) => {
const pathname = getPathname(req.url);
const method = req.method.toUpperCase();
res.writeHead(404);
res.end(`[${method}] /${pathname} Not found!`);
};

// routes handle function
const routes = (req, res) => {
const pathname = getPathname(req.url);
const method = req.method.toLowerCase();
const route = routeRules[pathname];

if (route[method]) {
route[method](req, res);
} else {
routeRules.notFound(req, res);
}
};


// export
module.exports = routes;

0 comments on commit f9a388f

Please sign in to comment.