From 9867003f959594ed3bbeb8f8ebd382889a9372e3 Mon Sep 17 00:00:00 2001 From: seognil LC Date: Tue, 26 May 2020 15:30:31 +0800 Subject: [PATCH] feat(add): node http --- node/http-server/package.json | 11 ++++++ node/http-server/public/404.html | 16 ++++++++ node/http-server/public/about.html | 16 ++++++++ node/http-server/public/index.html | 16 ++++++++ node/http-server/public/style/style.css | 3 ++ node/http-server/src/index.js | 51 +++++++++++++++++++++++++ node/http-server/tsconfig.json | 1 + readme.md | 6 ++- 8 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 node/http-server/package.json create mode 100644 node/http-server/public/404.html create mode 100644 node/http-server/public/about.html create mode 100644 node/http-server/public/index.html create mode 100644 node/http-server/public/style/style.css create mode 100644 node/http-server/src/index.js create mode 100644 node/http-server/tsconfig.json diff --git a/node/http-server/package.json b/node/http-server/package.json new file mode 100644 index 0000000..e5c7ac4 --- /dev/null +++ b/node/http-server/package.json @@ -0,0 +1,11 @@ +{ + "scripts": { + "start": "node src/index.js", + "dev": "nodemon src/index.js" + }, + "devDependencies": { + "@types/node": "^14.0.5", + "@types/nodemon": "^1.19.0", + "nodemon": "^2.0.4" + } +} diff --git a/node/http-server/public/404.html b/node/http-server/public/404.html new file mode 100644 index 0000000..79c3c90 --- /dev/null +++ b/node/http-server/public/404.html @@ -0,0 +1,16 @@ + + + + + + + Document + + +

404 Not Found

+
+ Home + About + 404 + + diff --git a/node/http-server/public/about.html b/node/http-server/public/about.html new file mode 100644 index 0000000..d43ec04 --- /dev/null +++ b/node/http-server/public/about.html @@ -0,0 +1,16 @@ + + + + + + + Document + + +

About Page

+
+ Home + About + 404 + + diff --git a/node/http-server/public/index.html b/node/http-server/public/index.html new file mode 100644 index 0000000..95fe15f --- /dev/null +++ b/node/http-server/public/index.html @@ -0,0 +1,16 @@ + + + + + + + Document + + +

Home Page

+
+ Home + About + 404 + + diff --git a/node/http-server/public/style/style.css b/node/http-server/public/style/style.css new file mode 100644 index 0000000..de116f5 --- /dev/null +++ b/node/http-server/public/style/style.css @@ -0,0 +1,3 @@ +html { + background-color: hsla(0, 70%, 70%, 0.3) +} diff --git a/node/http-server/src/index.js b/node/http-server/src/index.js new file mode 100644 index 0000000..0107c5b --- /dev/null +++ b/node/http-server/src/index.js @@ -0,0 +1,51 @@ +const path = require('path'); +const fs = require('fs'); +const http = require('http'); + +// * ---------------- + +const htmlFolder = path.resolve(__dirname, '../public'); +const notFoundPath = path.resolve(htmlFolder, '404.html'); + +const getFilePath = (url) => { + const ext = path.extname(url); + const missingExt = ext ? '' : '.html'; + const validUrl = url === '/' ? '/index' : url; + const filePath = htmlFolder + validUrl + missingExt; + + return filePath; +}; + +const contentTypeMap = { + '.css': 'text/css', + '.html': 'text/html', +}; + +const getContentTypeHeader = (filePath) => ({ + 'Content-Type': contentTypeMap[path.extname(filePath)] || '', +}); + +http + .createServer(async (req, res) => { + const filePath = getFilePath(req.url); + + fs.readFile(filePath, (err, content) => { + if (err) { + if (err.code === 'ENOENT') { + fs.readFile(notFoundPath, (err, content) => { + res.writeHead(200, getContentTypeHeader(notFoundPath)); + res.end(content, 'utf8'); + }); + } else { + res.writeHead(500); + res.end(`Server Error: ${err.code}`); + } + } else { + res.writeHead(200, getContentTypeHeader(filePath)); + res.end(content, 'utf8'); + } + }); + }) + .listen(8100, () => { + console.log('http://localhost:8100/'); + }); diff --git a/node/http-server/tsconfig.json b/node/http-server/tsconfig.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/node/http-server/tsconfig.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/readme.md b/readme.md index a4132d4..7ab71c0 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# LEARNING BY DOING +# LEARN BY DOING ## What is it 这是什么 @@ -24,11 +24,13 @@ Because it's the only way to learn how to code -- by practice - [ ] useful functions - [ ] tricks - [ ] polyfills + - **Node.js** + - [x] [http](./node/http-server/):搭建一个简单的服务器 - **JS lib usage** - [x] [react hooks](./react/) - [x] [redux](./redux/) - [x] [rxjs](./rxjs/) - - [ ] Webpack + - [x] [Webpack](https://github.com/seognil-study/webpack-playground) - [**Testing**](./testing/) - [x] [Jest](./testing/jest/) - [x] [Testing-Library](./testing/testing-library/)