๐ฅ [Node.js] Node.js ๋ฅผ ์ฌ์ฉํ Server ๊ตฌํ
Server ๊ตฌํ ์์ ๊ธฐ๋ณธ ์ค์ ๊ณผ ์๋ฒ ๊ตฌํ๋ฒ์ ๋ํด ์์๋ณด๋ ํฌ์คํธ์ ๋๋ค.
CJS
CJS ๋?
Common JS
์ ์ฝ์๋ก ์์ JavaScript ๋ง์ ์ฌ์ฉํ ์๋ฒ ๊ตฌํ์ ๋ปํจ
- ํ์ผ ๋จ์๋ก ์บก์ํ
require
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋ฅธmodule(package)
๋ฅผ ๋ถ๋ฌ์ดmodule.exports
๋exports
๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋์ ํจ์๋ ๊ฐ์ฒด๋ฅผ ์ธ๋ถ์ ๊ณต๊ฐํจ
1
2
3
4
5
6
7
8
9
10
11
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Hello Node.js! CJS!");
res.end();
});
server.listen(3000, () => {
console.log("Server is listening on port 3000 ...");
});
ESM
ECMAScript Module
์ ์ฝ์๋ก JavaScript ์ ๊ณต์ ๋ชจ๋ ์์คํ
import
๋ฌธ์ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ๋ชจ๋์ด๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ธฐ๋ฅ์ ๋ถ๋ฌ์ดexport
๋ฌธ์ ์ฌ์ฉํ์ฌ ๋ชจ๋์ ํจ์, ํด๋์ค, ๋ณ์ ๋ฑ์ ์ธ๋ถ์ ๊ณต๊ฐESM
์ ๋ชจ๋ ๋ก๋ฉ์ ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์์- ์ฝ๋์ ๊ตฌ์กฐ๋ฅผ ๋ ๋ช ํํ๊ฒ ํ๊ณ , ๋ชจ๋ ๊ฐ์ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ์ฉ์ด
1
2
3
4
5
6
7
8
9
{
"name": "my-app",
"version": "1.0.0",
"description": "test node.js folder",
"main": "index.js",
"type": "module"
// ์ค๋ต...
}
์ฌ์ฉํ๊ธฐ ์ํด
type
์module
๋ก ๊ฑธ์ด์ฃผ์ด์ผ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { createServer } from "http";
// ๋ด๊ฐ ์ํ๋ ๋ชจ๋์ ์ฌ์ฉํ๋ค. import ๋ฌธ๊ณผ ๋์ผ
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("Hello Node.js! ESM!");
// ํญ์ response ๋ฅผ ์ฌ์ฉํด ์๋ตํ ๋๋
res.end();
// ์๋ต์ ๋๋ด์ฃผ์ด์ผ ํ๋ค!
});
server.listen(3000, () => {
console.log("Server is listening port 3000 ...");
});
Nodemon
Node.js
์๋ฒ๋ฅผ ํฌ ๋, ์ฝ๋ ๋ณ๊ฒฝ์ด ์ฆ๊ฐ ๋ฐ์๋์ง ์๋๋ค.์ฆ๊ฐ ๋ฐ์๋๊ฒ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
1
npm install nodemon -D
Express ๋?
Express
๋Node.js
์ ํต์ฌ ๋ชจ๋์ธHTTP
๋ฅผ ์ถ์ํํ์ฌ ๋ผ์ฐํ , ์์ฒญ ์ฒ๋ฆฌ, ์ค๋ฅ ๊ด๋ฆฌ ๋ฑ๊ธฐ๋ฅ์ ๊ฐ๊ฒฐํ๊ณ ํจ์จ์ ์ธ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์๋๋ก ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ฆ,
Express
๋ ์ฌ๋ฌmiddleware
์ ์ฐ๊ฒฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import express from 'express';
const app = express();
const port = 3000;
// ๋ฃจํธ ๊ฒฝ๋ก์ ๋ํ GET ์์ฒญ ์ฒ๋ฆฌ
app.get('/', (req, res) => {
res.send('Hello World!');
});
// ์๋ฒ ์์
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
FS
File System
์ ์๋ฏธํ๋ฉฐ, ํ์ผ ์์คํ ์ ์ ๊ทผํ์ฌํ์ผ์ ์์ฑ, ์ฝ๊ธฐ, ์ฐ๊ธฐ, ์ญ์ ๋ฐ ์์ ๋ฑ์ ์์ ์ ์ํํ ์ ์๋ API๋ฅผ ์ ๊ณต
Template Engine
ํ์ด์ง๋ฅผ ๋ง๋ค ๋๋ง๋ค Rendering ํ html ํ์ผ์ ๊ณ์ํด์ ์์ฑํ ์ ์๋ค.
๋ฐ๋ผ์
Template
ํ ํ์ฌ ์ฌ์ฉํด์ผ Code ์ฌ์ฌ์ฉ์ฑ๋ ๋์์ง๋ฉฐ ๊ฐ๋ ์ฑ๋ ์ข์์ง๋ค.์ด๋ฌํ ๋์์ ๋์์ฃผ๋ ๊ฒ์ด
Template Engine
์ด๋ค.
Mustache
,Pug
,Nunjucks
๊ฐ ๋ํ์ ์ด๋ฉฐ ์ฐ์ต์์๋Nunjucks
๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๋ค.
Server ๋์ ๊ธฐ์ด ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import express from "express";
import nunjucks from "nunjucks";
import bodyParser from "body-parser";
import fs from "fs";
import path from "path";
const app = express();
const __dirname = path.resolve();
// file path
// my-app/data/writing.json ์ผ๋ก ๊ฒฝ๋ก ์ง์
const filePath = path.join(__dirname, "data", "writing.json");
// body parser set
app.use(bodyParser.urlencoded({ extended: false })); // express ๊ธฐ๋ณธ ๋ชจ๋ ์ฌ์ฉ
app.use(bodyParser.json());
// view engine set
app.set("view engine", "html"); // main.html -> main(.html)
// nunjucks
nunjucks.configure("views", { // views folder ๋ด์ ์๋ ๋ณ๊ฒฝ ๊ฐ์ watch
watch: true, // html ํ์ผ์ด ์์ ๋ ๊ฒฝ์ฐ, ๋ค์ ๋ฐ์ ํ ๋ ๋๋ง
express: app,
});
// middleware
// main page GET
app.get("/", (req, res) => {
const fileData = fs.readFileSync(filePath);
const writings = JSON.parse(fileData);
console.log(writings);
res.render("main", { list: writings });
// main.html ์ list ์ props ์ ๋ฌ
// list ๋ ์ด์ nunjucks ์ ์ํด ๊ฐ์ ํ์ฉ ๊ฐ๋ฅ
});
app.get("/write", (req, res) => {
res.render("write");
});
app.post("/write", async (req, res) => {
const title = req.body.title;
const contents = req.body.contents;
const date = req.body.date;
// data ์ ์ฅ
// data/wriring.json ์ ๊ฐ ์ ์ฅ
const fileData = fs.readFileSync(filePath);
console.log(fileData); // ํ์ผ ์ฝ๊ธฐ
const writings = JSON.parse(fileData);
console.log(writings); // ํ์ผ ๋ณํ
// request ๋ฐ์ดํฐ ์ ์ฅ
writings.push({
title: title,
contents: contents,
date: date,
});
// data/writing.json ์ ์ ์ฅํ๊ธฐ
fs.writeFileSync(filePath, JSON.stringify(writings));
res.render("detail", {
detail: { title: title, contents: contents, date: date },
});
});
app.get("/detail", async (req, res) => {
res.render("detail");
});
app.listen(3000, () => {
console.log(`
โโโโ โโโ โโโโโโโ โโโโโโโ โโโโโโโโ โโโโโโโโโโโ
โโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโ
โโโโโโ โโโโโโ โโโโโโ โโโโโโโโโ โโโโโโโโโโโ
โโโโโโโโโโโโโ โโโโโโ โโโโโโโโโ โโ โโโโโโโโโโโ
โโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ โโโโโ โโโโโโโ โโโโโโโ โโโโโโโโโโโ โโโโโโ โโโโโโโโ
`);
});
1
2
3
4
[
{ "title": "HELLO", "contents": "123", "date": "" },
{ "title": "๊ธ ์์ฑ", "contents": "์์ฑ์ค...", "date": "2024-02-28" }
]
(์ ์์ ์ผ๋ก json ์ data ๊ฐ ๋ค์ด๊ฐ ๊ฒ์ ๋ณผ ์ ์์)