studies/Front-end

[Node.js] express κ°„νŽΈ μ„œλ²„ μ‹€ν–‰ν•˜κΈ°, μ„œλ²„ μ‹€ν–‰ 파일 λ§Œλ“€κΈ° (VS CODE)

Vada Kim 2021. 3. 10. 21:59
728x90
λ°˜μ‘ν˜•

express μ„€μΉ˜

 

μ΅μŠ€ν”„λ ˆμŠ€κ°€ μ™œ ν•„μš”ν•œκ°€?

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

이것은 node.js 곡식 λ¬Έμ„œ μ•ˆλ‚΄ νŽ˜μ΄μ§€μ˜ μ„œλ²„ 생성 μ½”λ“œμ΄λ‹€.

 

그리고 μ•„λž˜λŠ” expressλ₯Ό μ‚¬μš©ν•œ μ„œλ²„ 생성 μ½”λ“œμ΄λ‹€.

const express = require('express');
const app = express();

const hostname = 'localhost';
const port = '3000';

app.listen(3000, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

 

 

 

vs code의 terminal을 μ—΄κ³ 

1.

npm init

κ²°κ³Ό 첫쀄에 creating a package.json file이 있으면 잘 μž‘λ™ν•œ 것.

 

μ΄μ–΄μ„œ μ—”ν„°λ₯Ό 계속 λˆ„λ₯΄λ©΄ λ˜λŠ”λ°, entry point λ¬ΌμŒμ—μ„œ μ„œλ²„μ½”λ“œλ₯Ό μž‘μ„±ν•œ js파일λͺ…을 μž‘μ„±ν•΄μ€€λ‹€. ex: server.js

λͺ¨λ‘ μ—”ν„°ν•˜λ©΄ package.json 파일이 λ£¨νŠΈμ— 생성됨.

 

2.

npm install express

λ‹€μš΄λ‘œλ“œ ν›„ λ£¨νŠΈμ— node_modules 폴더가 μƒμ„±λœλ‹€.

 

 

 

+

μ„œλ²„μ‹€ν–‰ μžλ™ν™”

npm install -g nodemon
nodemon server.js

 

 

 

 

μ„œλ²„ μ½”λ“œ 생성

const express = require('express');
const app = express();

const hostname = 'localhost';
const port = '3000';

app.listen(3000, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

μ˜ˆμ‹œ.

728x90
λ°˜μ‘ν˜•