Node.js Express

Yongjun Parkยท2022๋…„ 9์›” 15์ผ
0

2022 OSAM ํ•ด์ปคํ†ค

๋ชฉ๋ก ๋ณด๊ธฐ
6/11

2022 OSAM ํ•ด์ปคํ†ค ์‚ฌ์ „ ์˜จ๋ผ์ธ ๊ต์œก์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
๋ชจ๋ฅด๋Š” ๋‚ด์šฉ๋งŒ ๋ฐœ์ทŒํ•˜์—ฌ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด์šฉ์˜ ์—ฐ๊ฒฐ์„ฑ์ด ๋ถ€์กฑํ•œ ์  ์–‘ํ•ด ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

npm install express

์›น ์„œ๋ฒ„

์ •์  ํŽ˜์ด์ง€์™€ ๋™์  ํŽ˜์ด์ง€

Web Server์™€ WAS์˜ ์ฐจ์ด | ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Tech Interview

  • ์ •์  ํŽ˜์ด์ง€ : ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ํŽ˜์ด์ง€ ex) HTML, CSS, JS, image, video
  • ๋™์  ํŽ˜์ด์ง€ : ์ธ์ž์— ๋”ฐ๋ผ ๋ฐ”๋€Œ๋Š” ํŽ˜์ด์ง€ ex) JSP/Servlet

์›น ์„œ๋ฒ„ ์˜ˆ์ œ (template engine๋„ ํ™œ์šฉ)

const express = require("express");
const path = require("path");

const app = express();

// static ํด๋” : ์ •์  ํŒŒ์ผ ๋ชจ์Œ
const publicPath = path.join(__dirname + "./static");
app.use(express.static(publicPath));

// templates ํด๋” : hbs ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ html ํŒŒ์ผ ์ž‘์„ฑ
const viewPath = path.join(__dirname + "./templates");
app.set("view engine", "hbs");
app.set("views", viewPath);

app.get("", function(req, res) {
//	res.send("Hello World");
	res.render("index", {
		title: "Hello World",
		contents: "Hello World Body"
	});
});

app.listen(3000), () => {
	console.log("Server is online on port 3000");
});
<html>
<head>
	<title>{{title}}</title>
</head>
<body>
	<h1>{{contents}}</h1>
</body>
</html>

app.use(express.static())

์‚ฌ์‹คโ€ฆ ์ด๊ฑธ ๋ฐฐ์šฐ๊ณ  ๋ฒ™์ช˜๋‹ค. 2๋…„ ์ „์— ์ƒํ™œ์ฝ”๋”ฉ Node.js ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ , Express ์—†์ด ํ•œ๋ฒˆ ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž! ๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ์งœ๋‹ค๊ฐ€ ํฌ๊ธฐํ•œ ์ ์ด ์žˆ์—ˆ๋‹ค. ๊ทธ๋•Œ ํฌ๊ธฐํ•œ ์ด์œ ๊ฐ€ ๋ฐ”๋กœ ์ •์  ํŒŒ์ผ์„ ์ผ์ผ์ด ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š”๊ฒŒ ๋งค์šฐ ๊ท€์ฐฎ์•„์„œ์˜€๋‹ค. ๊ทธ๋•Œ ์ด ๊ธฐ๋Šฅ์„ ์•Œ์•˜๋”๋ผ๋ฉดโ€ฆ

์˜ˆ๋ฅผ ๋“ค์–ด, localhost:3000/images/cat.jpg ๋กœ ์ ‘๊ทผํ•œ๋‹ค๋ฉด, __dirname + static/images/cat.jpg ๋ฅผ ์ฐพ์•„์„œ ์ž๋™์œผ๋กœ ๋ณด๋‚ด์ค€๋‹ค.

app.set()

  1. app.set('view engine', 'hbs') : hbs๋ฅผ template engine์œผ๋กœ ์“ฐ๊ฒ ์Šต๋‹ˆ๋‹ค.
  2. app.set('views', viewPath) : view ํŒŒ์ผ์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ์€ viewPath ์ž…๋‹ˆ๋‹ค.

API ์„œ๋ฒ„ - Github ์œ ์ € ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

// ์œ„์˜ ํŒŒ์ผ์—์„œ ์ˆ˜์ •๋œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑ
require("isomorphic-fetch");

app.use(express.json());

app.get("/github", async function(req, res) {
	const response = await fetch(`https://api.github.com/users/${req.query.user}`);
	res.json(await response.json());
});

app.user(express.json());
  • http://localhost:3000/github?user=yopark ์˜ ํ˜•์‹์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

MongoDB ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๋ฐ์ดํ„ฐ๋ฅผ Table ๋ง๊ณ  Document(JSON) ๊ธฐ๋ฐ˜์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

Connect

npm install mongodb

// connect.js

const { MongoClient } = require("mongodb");

const url = "MongoDB์— ๊ฐ€์ž…ํ•ด์„œ ๊ฐœ์ธ url์„ ๋ฐ›์œผ์„ธ์š”";
const client = new MongoClient(url);

async function main() {
	await client.connect(); 
	console.log("Connected successfully to server");

	return "done.";
}

main()
	.then(console.log) // res => console.log(res)
	.catch(console.error)
	.finally(() => client.close());

Create

// connect.js

const { MongoClient } = require("mongodb");

const url = "MongoDB์— ๊ฐ€์ž…ํ•ด์„œ ๊ฐœ์ธ url์„ ๋ฐ›์œผ์„ธ์š”";
const client = new MongoClient(url);

async function main() {
	await client.connect(); 
	console.log("Connected successfully to server");

	const db = client.db("oss"); // db๋ช…
	const collection = db.collection("documents"); // table๋ช…

	const insertResult = await collections.insertMany([
		{ a: 1 },
		{ a: 2 },
		{ a: 3 }
	]);
	return insertResult;
}

main()
	.then(console.log) // res => console.log(res)
	.catch(console.error)
	.finally(() => client.close());

Select

// ์œ„ ํŒŒ์ผ์—์„œ insertMany ํ•จ์ˆ˜ ๋ถ€๋ถ„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋ฉฐ, 
// ๋‚˜๋จธ์ง€ ๋‚ด์šฉ์€ ๋ชจ๋‘ ๊ฐ™๋‹ค. 

const findResult = await collection.find({}).toArray();
console.log("Found documents => ", findResult);

const filteredDocs = await collection.find({ a: 3 }).toArray();
console.log("Found documents filtered by { a: 3 } => ", filteredDocs);

Update

const updateResult = await collection.updateOne({ a: 3}, {$set: { b: 1} });
  • **$set ์„ ๋„ฃ์ง€ ์•Š์œผ๋ฉด document๋ฅผ ํ†ต์งธ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ! ์ฃผ์˜**
  • $set์„ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐ™์€ ํ‚ค๋Š” ์—…๋ฐ์ดํŠธ๋˜๊ณ , ์—†๋˜ ํ‚ค๋Š” ์ถ”๊ฐ€๋œ๋‹ค.

Delete

const deleteResult = await collection.deleteMany({ a: 3 });
profile
์ถ”์ƒํ™”๋˜์—ˆ๋˜ ๊ธฐ์ˆ ์„ ๋ฐ‘๋‹จ๊นŒ์ง€ ์ดํ•ดํ–ˆ์„ ๋•Œ์˜ ์พŒ๊ฐ์„ ์žŠ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€