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
๋ฐ์ํ