# 12/1 部課 ## 複習NodeJS https://tw.alphacamp.co/blog/node-js-and-javascript ```javascript= const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); ``` [11/24 部課](/qRXJwu0lQbCtiqURJGNvSQ) ### TODO https://jsonplaceholder.typicode.com/ Put TODO list's codes into Node Server: https://stackoverflow.com/questions/38757235/express-how-to-send-html-together-with-css-using-sendfile ### Notes https://pala.tw/frontend-backend-basic/ https://tecky.io/en/blog/Backend%E8%BC%83Frontend%E9%9B%A3%E5%97%8E/ https://progressbar.tw/posts/305 https://yakimhsu.com/project/project_w4_Network_http.html [HTTP codes](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) [HTTP cat](https://http.cat/) --- ### 課後補充 In `server.js`: ```javascript= app.route("/method") .get((req, res) => { res.send("method get"); }) .post((req, res) => { res.send("method post"); }) .put((req, res) => { res.send("method put"); }) .delete((req, res) => { res.send("method delete"); }); app.listen(3000); ``` Postman: `GET`: ![image](https://hackmd.io/_uploads/ryYK-FvST.png) `POST`: ![image](https://hackmd.io/_uploads/BkoobFwH6.png) `PUT`: ![image](https://hackmd.io/_uploads/H1Q0WtvHT.png) `DELETE`: ![image](https://hackmd.io/_uploads/S1F1MKvBa.png) `OPTIONS`: show methods ![image](https://hackmd.io/_uploads/HJCmMtwBT.png) Here is the tree structure in `class03_NodeJS/example-server/static`: ``` static ├── index.html ├── styles.css ├── todo.js └── img └── x.png ``` ```javascript= // using express static method app.use(express.static("static")); ``` ```javascript= // using sendFile method // cons: need to write the repeating method for every files app.get("/", (req, res) => { res.sendFile(path.join(__dirname, "/static/index.html")); }); app.get("/styles.css", (req, res) => { res.sendFile(path.join(__dirname, "/static/styles.css")); }); app.get("/todo.js", (req, res) => { res.sendFile(path.join(__dirname, "/static/todo.js")); }); app.get("/img/x.png", (req, res) => { res.sendFile(path.join(__dirname, "/static/img/x.png")); }); ```