Search

express ์†Œ๊ฐœ ๋ฐ ์„ค์น˜

express

Node.js๋ฅผ ์œ„ํ•œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

์ •์˜

โ€ข
๋น ๋ฅด๊ณ  ๊ฐ„ํŽธํ•œ ๋ฐฉ์‹์œผ๋กœ ์›น ๋ฐ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
โ€ข
๋ผ์šฐํŒ…, ๋ฏธ๋“ค์›จ์–ด, ํ…œํ”Œ๋ฆฟ ์—”์ง„ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ์œ ์—ฐํ•˜๊ฒŒ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š”๊ธฐ๋Šฅ

๊ธฐ๋Šฅ
์„ค๋ช…
์ตœ์†Œํ•œ์˜ ๊ธฐ๋Šฅ
ํ•ต์‹ฌ์ ์ธ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋Šฅ๋งŒ ์ œ๊ณตํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž์‹ ์˜ ์š”๊ตฌ์— ๋งž๊ฒŒ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‰ฝ๊ฒŒ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ผ์šฐํŒ…
์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ํŠน์ • ํ•ธ๋“ค๋Ÿฌ๋กœ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
๋ฏธ๋“ค์›จ์–ด
์š”์ฒญ๊ณผ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋กœ๊น…, ์ธ์ฆ, ์ฝ˜ํ…์ธ  ํ˜‘์ƒ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ…œํ”Œ๋ฆฟ
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋™์  ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ
๋ชจ๋“ˆ์‹ ์„ค๊ณ„์™€ ํ™œ๋ฐœํ•œ ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ๋•๋ถ„์— ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.

express ์„ค์น˜

์‚ฌ์ „ ์„ค์น˜ : node
express ๋Š” npm ๋ช…๋ น์œผ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
npm install express
Shell
๋ณต์‚ฌ

express ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰

๐Ÿ“ฆ start |-- ๐Ÿ“ฆpublic | |-- ๐Ÿ“„ index.html | |-- ๐Ÿ“„ styles.css |-- ๐Ÿ“„ app.js |-- ๐Ÿ“„ package.json
JavaScript
๋ณต์‚ฌ
1.
npm init
2.
app.js
3.
index.html
4.
package.json

npm init

npm init
Shell
๋ณต์‚ฌ
npm init ๋ช…๋ น์–ด๋Š” Node.js ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋กœ, package.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ ํ”„๋กœ์ ํŠธ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์™€ ์˜์กด์„ฑ ํŒจํ‚ค์ง€ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. npm init์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์„ค์ •์„ ์ž…๋ ฅ๋ฐ›์•„ package.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ, Express.js ํ”„๋กœ์ ํŠธ์—์„œ๋„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

app.js

const express = require('express'); const path = require('path'); const app = express(); const port = 3000; // ์ •์  ํŒŒ์ผ ์ œ๊ณต app.use(express.static(path.join(__dirname, 'public'))); // ๋ฃจํŠธ ๊ฒฝ๋กœ์— ์ ‘๊ทผํ•˜๋ฉด index.html์„ ์‘๋‹ต app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
JavaScript
๋ณต์‚ฌ

./public/index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Express.js Example</title> </head> <body> <h1>Hello, Express.js!</h1> </body> </html>
HTML
๋ณต์‚ฌ

package.json

{ "name": "express", "version": "1.0.0", "description": "hello express", "main": "app.js", "scripts": { "start": "node app.js" }, "author": "joeun", "license": "ISC", "dependencies": { "express": "^4.18.2" } }
JavaScript
๋ณต์‚ฌ

์„œ๋ฒ„ ์‹คํ–‰

node app
Shell
๋ณต์‚ฌ
npm start
Shell
๋ณต์‚ฌ
package.json ์˜ scripts ์†์„ฑ์˜ start ๋ช…๋ น์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.