Node JS ,express ํŒจํ‚ค์ง€๋กœ ์›น์„œ๋ฒ„ ๊ตฌ๋™ํ•˜๊ธฐ
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋””์ง€ํ„ธ ๊ฟ€ํŒ

Node JS ,express ํŒจํ‚ค์ง€๋กœ ์›น์„œ๋ฒ„ ๊ตฌ๋™ํ•˜๊ธฐ

by ์ผ์ƒ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค 2022. 3. 24.

 

 

NodeJs ๊ธฐ์ดˆ ์•Œ์•„๊ฐ€๊ธฐ

 

์ž๋™ํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” Selenium ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ node ,python์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น์„ ์ปจํŠธ๋กค ํ•ฉ๋‹ˆ๋‹ค. ํฌ๋กฌ๋„ ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— GAS (Google App Script) ๋กœ Chrome์„ ์ œ์–ดํ•  ์ˆ˜ ์—†๋‚˜ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ๋ช‡ ์ผ๋™์•ˆ ๋ฆฌ์„œ์น˜๋ฅผ ํ•˜์˜€์ง€๋งŒ ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. python์€ ์ด๋ฏธ ๋‹ค๋ฅธ ์œ ์ €๋ถ„๋“ค์ด ๊ฐœ๋ฐœํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— node.js๋กœ ์ž‘์—…ํ•˜๊ธฐ๋กœ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ €๋Š” ๋ณ„๋„๋กœ ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ nodejs๋กœ ํ•  ์˜ˆ์ •์ด๋ผ ์ด๋ฒˆ ๊ธฐํšŒ์— ์ตํ˜€๋‘˜๋ ค๊ณ  ํ•˜๋‹ˆ ์ฒ˜์Œ ํ•˜์‹œ๋Š” ๋ถ„๋“ค๋„ ์ €์™€ ํ•จ๊ป˜ ํ•™์Šตํ•ด๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค.

 

1๋‹จ๊ณ„. Node.js ์„ค์น˜

 

๋‹ค์šด๋กœ๋“œ | Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

Node JS๋กœ ์›น ์„œ๋ฒ„ ๊ตฌ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋ด…๋‹ˆ๋‹ค. ์ €๋Š” Mac์‚ฌ์šฉ์ด๋ฉด์„œ Visual code ํˆด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„ Nodejs ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•˜๋ฉด Windows๋‚˜ Mac์ด๋‚˜ ๋‹ค ์„ค์น˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

 

2๋‹จ๊ณ„. Visual Code ์„ค์น˜

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

์—๋””ํ„ฐ ํ˜•์‹์˜ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ์ฝ”๋”ฉํ•œ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ฃผ๊ณ  ์ €์žฅํ•ด์ฃผ๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํˆด์ž…๋‹ˆ๋‹ค. ๊ฒ€์€์ƒ‰ ๋ฐ”ํƒ•์— ํฐ ๊ธ€์”จ๋‹ค ์ƒ๊ฐํ•˜์—ฌ ์–ด๋ ค์›Œ ๋งˆ์‹œ๊ณ , ํด๋ฆญํ•˜์—ฌ ์•„๋ž˜ ์ €์™€ ํ•จ๊ป˜ ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

3๋‹จ๊ณ„. ํ„ฐ๋ฏธ๋„ ์—ด์–ด์„œ express ๋ชจ๋“ˆ ์„ค์น˜

npm init

์œ„ ๋ช…๋ น์–ด ์ž…๋ ฅ ํ›„ Enter๋ฅผ ๊ณ„์† ์ž…๋ ฅ ํ›„ "Yes" ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

npm install express --save

 

 

4๋‹จ๊ณ„. Visual Code > Open Folder > express

5๋‹จ๊ณ„. index.js or app.js ์ž‘์„ฑ ๋ฐ index.html ์ž‘์„ฑ

5-1. ์•„๋ž˜์™€ ๊ฐ™์ด index.js ํŒŒ์ผ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” 3000 ํฌํŠธ → 80 ํฌํŠธ๋กœ ๋ณ€๊ฒฝํ•˜์˜€์ง€๋งŒ, ๊ธฐ๋ณธ์„ค์ •์— ๋งž๊ฒŒ 3000 ํฌํŠธ๋กœ ์„ค์ •ํ•˜์—ฌ๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ํฌํŠธ๋Š” ์ˆซ์ž์ผ ๋ฟ ํฐ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. 

/*!
 * express
 * Copyright(c) 2009-2013 TJ Holowaychuk
 * Copyright(c) 2013 Roman Shtylman
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 */

'use strict';

var express= require('./lib/express');

var app = express()

// 3000 ํฌํŠธ๋กœ ์„œ๋ฒ„ ์˜คํ”ˆ
app.listen(3000, function() {
    console.log("start! express server on port 3000")
})

app.get('/', function(req,res) {
    res.sendFile(__dirname + "/public/index.html")
})

// localhost:3000/main ๋ธŒ๋ผ์šฐ์ €์— res.sendFile() ๋‚ด๋ถ€์˜ ํŒŒ์ผ์ด ๋„์›Œ์ง„๋‹ค.
app.get('/index', function(req,res) {
    res.sendFile(__dirname + "/public/index.html")
})
๋ฐ˜์‘ํ˜•


5-2. public/index.html ์„ ์ƒ์„ฑํ•ด๋ด…์‹œ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>main.html</title>
</head>
<body>
    <h1>main page</h1>

    <p>Node Express Package</p>
</body>
</html>

 

6๋‹จ๊ณ„.  ๋‹ค์‹œ ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ node js ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— nodemon package๋ฅผ ์„ค์น˜

npm install nodemon

์†Œ์Šค ์ˆ˜์ • ์—†์ด ์„œ๋ฒ„๋ฅผ ๋‚ด๋ ธ๋‹ค ์˜ฌ๋ฆด ํ•„์š” ์—†์ด "rs"๋ช…๋ น์–ด๋กœ ์„œ๋ฒ„๊ฐ€ ์ˆ˜์ •๋œ ๋ฒ„์ „์œผ๋กœ ์žฌ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ฐ„ํ˜น ๋‹ค๋ฅธ ํƒ€์ž๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ž˜๋ชป ๋ช…๋ ฅ์–ด๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒƒ ์•„๋‹Œ์ง€ ๊ฑฑ์ •๋  ๊ฒฝ์šฐ Enter๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋‹จ์„ ๋„์šฐ๊ณ  "rs" ๋ช…๋ น์–ด๋ฅผ ์žฌ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

7๋‹จ๊ณ„. ์ด์ œ ํ„ฐ๋ฏธ๋„์—์„œ ์„ค์น˜๋œ ํด๋”๋กœ ๊ฐ€์„œ index.js๋ฅผ ๊ตฌ๋™

nodemon index.js

 

์›นํŽ˜์ด์ง€๊ฐ€ ๋œจ๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋„๋ฉ”์ธ์„ ๊ฑธ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋„๋ฉ”์ธ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ฌ๋Ÿฌ๋ถ„์€ https://localhost:3000 ์œผ๋กœ ์ ‘์†ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™๋˜๋Š”๊ฑธ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก  package ์„ค์น˜ ์ค‘  npm fund ์ด๋Ÿฐ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œฌ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

> npm install --no-fund ๋‚˜์˜ Package Name ์„ ์‹คํ–‰ํ•ด์ค€๋‹ค.

 

๋ˆ„๊ตฌ๋‚˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ž์‹ ๋งŒ์˜ ์›นํŽ˜์ด์ง€๋ฅผ ์ œ์ž‘ํ•ด ๋ด…์‹œ๋‹ค. ๋ฌธ์˜ ์‚ฌํ•ญ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ํ”ผ๋“œ๋ฐฑ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. 

๋Œ“๊ธ€