forked from pirple/The-NodeJS-Master-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Skye.Wu <[email protected]>
- Loading branch information
Skye.Wu
committed
Oct 29, 2018
1 parent
728dd39
commit f9a388f
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |