πŸ‘©‍πŸ’» λ°±μ—”λ“œ(Back-End)/Node js

[Express μ›Ή μ„œλ²„ λ§Œλ“€κΈ°] (1) express λͺ¨λ“ˆλ‘œ μ„œλ²„ κ΅¬μΆ•ν•˜κΈ°

μ§•μ§•μ•ŒνŒŒμΉ΄ 2023. 4. 23. 23:28
728x90
λ°˜μ‘ν˜•

<λ³Έ λΈ”λ‘œκ·ΈλŠ” Node.js κ΅κ³Όμ„œλ₯Ό μ°Έκ³ ν•΄μ„œ κ³΅λΆ€ν•˜λ©° μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€ :-)>

 

πŸŽ€ express λͺ¨λ“ˆλ‘œ μ„œλ²„ κ΅¬μΆ•ν•˜κΈ° 

🌈 app.js

// express : http λͺ¨λ“ˆ λ‚΄μž₯λ˜μ–΄μ„œ μ„œλ²„μ˜ μ—­ν• 
const express = require("express");
const app = express();
require("dotenv").config();

// μ„œλ²„κ°€ 싀행될 포트
// app.set("ν‚€, κ°’") : 데이터 μ €μž₯
app.set("port", process.env.PORT || 3000);

// app.get("μ£Όμ†Œ, λΌμš°ν„°") : μ£Όμ†Œμ— λŒ€ν•œ GET μš”μ²­μ΄ 올 λ•Œ μ–΄λ–€ λ™μž‘ 할지 적기
app.get("/", (req, res) => {
    res.send("Hello express");
});

// HTTP μ›Ή μ„œλ²„μ™€ 동일
app.listen(app.get("port"), () => {
    console.log(app.get("port"), "빈 ν¬νŠΈμ—μ„œ λŒ€κΈ°μ€‘");
});

 

πŸŽ€ HTML 둜 μ‘λ‹΅ν•˜κΈ° 

🌈 app.js

// express : http λͺ¨λ“ˆ λ‚΄μž₯λ˜μ–΄μ„œ μ„œλ²„μ˜ μ—­ν• 
const express = require("express");
const app = express();
// 파일 경둜
const path = require("path");
require("dotenv").config();

// μ„œλ²„κ°€ 싀행될 포트
// app.set("ν‚€, κ°’") : 데이터 μ €μž₯
app.set("port", process.env.PORT || 3000);

// app.get("μ£Όμ†Œ, λΌμš°ν„°") : μ£Όμ†Œμ— λŒ€ν•œ GET μš”μ²­μ΄ 올 λ•Œ μ–΄λ–€ λ™μž‘ 할지 적기
app.get("/", (req, res) => {
    res.sendFile(path.join(__dirname, "/index.html"));
});

// HTTP μ›Ή μ„œλ²„μ™€ 동일
app.listen(app.get("port"), () => {
    console.log(app.get("port"), "빈 ν¬νŠΈμ—μ„œ λŒ€κΈ°μ€‘");
});

 

🌈 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Express Server</title>
</head>
<body>
    <h1>Express</h1>
    <p>λ°°μ›Œλ³΄μž!</p>    
</body>
</html>

728x90
λ°˜μ‘ν˜•