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
λ°μν