์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋ ธ๋ง๋์ฝ๋
- ํ๋ก์ ํธ
- node.js
- ๋๋ฆผ์ฝ๋ฉ
- ์ฝ๋ฉ์ ํ
- ๋ฐฑ์ค
- ์ฝ๋ฉํ ์คํธ
- HTML
- error
- ํ์ด์ฌ
- ์ฝ๋ฉ
- ํฌ๋กค๋ง
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก๊ทธ๋๋จธ์ค
- ํ ์ดํ๋ก์ ํธ
- KDT
- Python
- ํ๋ก ํธ์๋
- CSS
- ๊ตญ๋น์ง์
- Til
- ๊ฐ๋ฐ
- ๋ชจ๊ฐ์ฝ
- heapq
- react
- mongodb
- JS
- ๊ทธ๋ฆฌ๋
- javascript
- fe
- Today
- Total
๐ฑ โ ๐ณ
[node.js] node.js ์ ๋ฌธ ๋ณธ๋ฌธ
๐ ํ๋ ์์ํฌ
ํธ๋ฆฌํจ, ๋ณด์์ด ์ข์
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> ์ด๋ ใฑใ ์ค์ ํ๊ฑฐ ๋ค ์ง์ฐ๊ธฐ
'Server > Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[node.js] Yarn์ด๋? (0) | 2022.09.02 |
---|---|
[node.js] ํ๋ ์์ํฌ ์์ด CRUD ๊ตฌํํด๋ณด๊ธฐ (0) | 2022.08.27 |
[node.js] class VS ์์ฑ์ ํจ์ (0) | 2022.08.26 |
[node.js] GET ์์ฒญ ์ฒ๋ฆฌ (1) | 2022.08.25 |
mac์์ node.js ๊ฐ๋ฐ ํ๊ฒฝ ์ธํ (0) | 2022.08.24 |