๐ŸŒฑ → ๐ŸŒณ

[node.js] node.js ์ž…๋ฌธ ๋ณธ๋ฌธ

Server/Node.js

[node.js] node.js ์ž…๋ฌธ

BAY 2022. 8. 22. 18:35
728x90

๐Ÿ“Œ ํ”„๋ ˆ์ž„์›Œํฌ 

ํŽธ๋ฆฌํ•จ, ๋ณด์•ˆ์ด ์ข‹์Œ 

 

ex) bootstrap - css ํ”„๋ ˆ์ž„์›Œํฌ(frontend)

spring - java ํ”„๋ ˆ์ž„์›Œํฌ(backend)

 

ํ”„๋ก ํŠธ์—”๋“œ๋กœ๋Š” ๊ฒฐ๊ณผ๋งŒ ๋ณด์—ฌ์คŒ 

 

๐Ÿ“Œ ์—๋Ÿฌ

์ปดํŒŒ์ผ์—๋Ÿฌ(์‹ ํ…์Šค์—๋Ÿฌ) : ๋ฌธ๋ฒ• ์˜ค๋ฅ˜

๋Ÿฐํƒ€์ž„์—๋Ÿฌ: ์‹คํ–‰๋˜๋Š” ๋„์ค‘์— ๋‹ค์šด๋˜๋Š” ์—๋Ÿฌ/ ๋ฌธ๋งฅ์ƒ ์˜ค๋ฅ˜

 

โœ… head vs body 

head์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ํ•œ ์ค„, ํ•œ ์ค„ ๋‚˜์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•œ๊บผ๋ฒˆ์— ๋‚˜์˜ด 

body๋Š” ํ•œ ์ค„, ํ•œ ์ค„ ๋‚˜์˜ด 

head์— div, span ,, ์ด๋Ÿฐ tag๋“ค ๋‹ค ์“ธ ์ˆ˜ ์žˆ์Œ 

body์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋„ˆ๊ทธ๋Ÿฝ๊ฒŒ ๋„˜์–ด๊ฐ€์ง€๋งŒ, head์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋ฉด ๊ฒฐ๊ณผ๋ฌผ์ด ๋กœ๋“œ๋˜์ง€ ์•Š์Œ 

 

๐Ÿ“Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ๋น„๋™๊ธฐ์‹

์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜

 

์›๋ž˜๋Š” ๋ฒก์—”๋“œ๊ฐ€ ์‹œ๋‚˜๋ฆฌ์˜ค ๊ธฐ๋ฐ˜์ด์˜€๋Š”๋ฐ

์ฑ„ํŒ… : ์‹œ๋‚˜๋ฆฌ์˜ค ์—†์Œ, ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ด๋ฃจ์–ด ์ ธ์•ผ ํ•จ 

๋Œ€๋ถ€๋ถ„ ํŽ˜์ด์ง€์— ์ฑ„ํŒ… ๊ธฐ๋Šฅ์ด ๋“ค์–ด๊ฐ€๋Š” ์ถ”์„ธ๊ฐ€ ๋จ

์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜์ด๋ฉด ์–ธ์–ด๋ฉด ์ด ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅ 

 

๐Ÿ“Œ node.js (server์—์„œ) ๊ตฌํ˜„ 

mkdir chat //chat์ด๋ผ๋Š” ํด๋” ์ƒ์„ฑ
cd chat
npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (chat) chat_project
version: (1.0.0)
description: very good super chat project
entry point: (index.js) index.js
test command:
git repository:
keywords:
author: So Min Lee
license: (ISC)
About to write to /Users/isomin/Documents/project/chat/package.json:

{
  "name": "chat_project",
  "version": "1.0.0",
  "description": "very good super chat project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "So Min Lee",
  "license": "ISC"
}


Is this OK? (yes)

 

vi : ๋ฆฌ๋ˆ…์Šค ๋ฒ„์ „ ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ 

 

โœ… node.js ๊ธฐ๋ฐ˜ ํ”„๋ ˆ์ž„์›Œํฌ : express

apt : ์šฐ๋ถ„ํŠธ์— ๊นŒ๋Š” ๊ฒƒ 

npm: ํ”„๋กœ์ ํŠธ์˜ package.json ์— ๊ธฐ๋ก๋œ ๋ชจ๋“  ์˜์กด์„ฑ ํŒจํ‚ค์ง€๋“ค์„ ๋‚ด๋ ค๋ฐ›๊ฒŒ ๋จ

npm install express
npm install ejs

 

โœ… MVC ํŒจํ„ด

: ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜ 

 

Model: DB ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„

View: FE ์ฝ”๋“œ ๊ด€๋ฆฌ ๋ถ€๋ถ„

Controller: BE ์ฝ”๋“œ ๊ด€๋ฆฌ ๋ถ€๋ถ„ 

 

express๋Š” MVC ํŒจํ„ด์„ ์“ฐ๋„๋ก ๊ฐ•์ œํ•จ

 

ejs: view file์„ ๊ด€๋ฆฌ ํ•ด์ฃผ๋Š” ๊ทœ์น™/ node.js์˜ ๊ฐ€์žฅ ์ƒ์šฉํ™”๋œ view engine

 

~ ls
node_modules      package-lock.json package.json

node_modules: ํŽ˜ํ‚ค์ง€ ๋ชจ๋“ˆ ๊ด€๋ฆฌ

 

โœ…  app.js ์ถ”๊ฐ€

const express = require("express"); // express ๋ชจ๋“ˆ ์š”์ฒญ
const ejs = require("ejs");         // ejs ๋ชจ๋“ˆ ์š”์ฒญ
const app = express();              // app์„ express ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ํ‚จ๋‹ค

app.set("view engine", "ejs");      // app์— view engine์„ ์„ค์น˜, ejs๋ฅผ ํ…œํ”Œ๋ฆฟ์œผ๋กœ
app.use(express.static(__dirname + '/')); // views ํด๋” ๊ฒฝ๋กœ๋Š” ํ”„๋กœ์ ํŠธ ํด๋”.(__dirname์ด ํด๋” ์œ„์น˜)

app.get("/test1", function(req, res){ 
    console.log("์•ˆ๋…• ํ…Œ์ŠคํŠธ ํŽ˜์ด์ง€!");
    res.render("test1"); 
}); // test1์ด๋ผ๋Š” ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜จ ์‚ฌ๋žŒ์—๊ฒŒ๋Š” test1์ด๋ผ๋Š” ํŒŒ์ผ์„ ๋ณด์—ฌ์ค˜๋ผ 

app.get("/test2", function(req, res){  
    console.log("์•ˆ๋…• ํ…Œ์ŠคํŠธ ํŽ˜์ด์ง€!");
    res.render("test2"); 
}); // test2์ด๋ผ๋Š” ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜จ ์‚ฌ๋žŒ์—๊ฒŒ๋Š” test2์ด๋ผ๋Š” ํŒŒ์ผ์„ ๋ณด์—ฌ์ค˜๋ผ

app.listen(3000, function(){
    console.log("test.js is running now..."); 
});

 

require(): ํŒจํ‚ค์ง€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ํ•จ์ˆ˜ 

๋ผ์šฐํŒ…: ๊ฒฝ๋กœ ์„ค์ • 

~ node app.js
test.js is running now...

 

// app console.log ์ฐ์–ด๋ณด๊น… 

 

๋‚ด ์„œ๋ฒ„ ex) www.thals.today  ๋ผ๋ฉด, www.thals.today:3000/test1  ์ด๋Ÿฐ์‹์œผ๋กœ ์ฃผ์†Œํ•ด๋ณด๋ฉด ๋จ

 

โœ… ํ”„๋กœ์ ํŠธ ์„œ๋ฒ„์— ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ๋ฒ• 

npm init 

views๋ผ๋Š” ํด๋” ์ƒ์„ฑ 

ํ”„๋กœ์ ํŠธ ํ–ˆ๋˜ ๋ชจ๋“  ํŒŒ์ผ views์— ๋‹ค ์˜ฎ๊ธฐ๊ณ  

๊ฒฝ๋กœ ์„ค์ • ๋‹ค ํ•˜๊ณ  <a href="b.html">click</a> ์ด๋ ‡ใ„ฑใ…” ์„ค์ •ํ•œ๊ฑฐ ๋‹ค ์ง€์šฐ๊ธฐ 

728x90