๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
studies/Front-end

[Node.js] express ๊ฐ„ํŽธ ์„œ๋ฒ„ ์‹คํ–‰ํ•˜๊ธฐ, ์„œ๋ฒ„ ์‹คํ–‰ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ (VS CODE)

by Vada Kim 2021. 3. 10.
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
๋ฐ˜์‘ํ˜•