๐Ÿ“š Study/JS, JQuery

์ต์Šคํ”„๋ ˆ์Šค๋กœ ์›น ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ

kkh1902 2022. 5. 14. 15:35
728x90
๋ฐ˜์‘ํ˜•
  • ์„œ๋ก 

http๋ชจ๋“ˆ๋งŒ ์‚ฌ์šฉํ•ด์„œ ์›น ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ๋Š” ๋งŽ์€ ๊ฒƒ๋“ค์„ ์ง์ ‘ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

express ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฏธ๋“ค์›จ์–ด, ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

express ์„œ๋ฒ„๋งŒ๋“ค๊ธฐ

$ npm init  ํ•ด์„œ package.json ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

  •  express, http ๋ชจ๋“ˆ์€ ./ ๊ธฐํ˜ธ ์—†์ด ๋ชจ๋“ˆ ์ด๋ฆ„๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ด๊ฒƒ์€ ๋…ธ๋“œ์— ๋‚ด์žฅ๋œ ๋ชจ๋“ˆ ๋˜๋Š” npm์œผ๋กœ ์„ค์น˜ํ•œ ์™ธ์žฅ ๋ชจ๋“ˆ์ผ ๋•Œ ์ƒ๋Œ€ ํŒจ์Šค๊ฐ€ ์•„๋‹Œ ์ด๋ฆ„๋งŒ ์ง€์ •ํ•˜๋„๋ก ์„ค์ •๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
// Express ๊ธฐ๋ณธ ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
var express = require("express");

http = require("http");

// ์ต์Šคํ”„๋ ˆ์Šค ๊ฐ์ฒด ์ƒ์„ฑ
var app = express();

// ๊ธฐ๋ณธ ํฌํŠธ๋ฅผ app ๊ฐ์ฒด์— ์†์„ฑ์œผ๋กœ ์„ค์ •
app.set("port", process.env.PORT || 3000);

// Express ์„œ๋ฒ„ ์‹œ์ž‘
http.createServer(app).listen(app.get("port"), function () {
    console.log("์ต์Šคํ”„๋ ˆ์Šค ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค : " + app.get("port"));
});

์ต์Šคํ”„๋ ˆ์Šค ์„œ๋ฒ„ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์š” ๋ฉ”์†Œ๋“œ

  • set(name, value) :  ์„œ๋ฒ„ ์„ค์ •์„ ์œ„ํ•œ ์†์„ฑ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. set() ๋ฉ”์†Œ๋“œ๋กœ ์ง€์ •ํ•œ ์†์„ฑ์€ get() ๋ฉ”์†Œ๋“œ๋กœ ๊บผ๋‚ด์–ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • get(name) : ์„œ๋ฒ„ ์„ค์ •์„ ์œ„ํ•ด ์ง€์ •ํ•œ ์†์„ฑ์„ ๊บผ๋‚ด ์˜ต๋‹ˆ๋‹ค.
  • use( [path,] function[, function...]) : ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • get( [path,] function) : ํŠน์ • ํŒจ์Šค๋กœ ์š”์ฒญ๋œ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์„œ๋ฒ„ ์„ค์ •์„ ์œ„ ํ•ด ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ์ฃผ์š” ์†์„ฑ

  • env : ์„œ๋ฒ„ ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • views : ๋ทฐ๋“ค์ด ๋“ค์–ด ์žˆ๋Š” ํด๋” ๋˜๋Š” ํด๋” ๋ฐฐ์—ด์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • view engine : ๋””ํดํŠธ๋กœ ์‚ฌ์šฉํ•  ๋ทฐ ์—”์ง„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฏธ๋“ค์›จ์–ด๋กœ ํด๋ผ์ด์–ธํŠธ์— ์‘๋‹ต๋ณด๋‚ด๊ธฐ

// Express ๊ธฐ๋ณธ ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
var express = require("express");

http = require("http");

// ์ต์Šคํ”„๋ ˆ์Šค ๊ฐ์ฒด ์ƒ์„ฑ
var app = express();

app.use(function (req, res, next) {
    console.log("์ฒซ ๋ฒˆ์งธ ๋ฏธ๋“ค์›จ์–ด์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•จ");

    req.user = "nike";

    next();
    // res.writeHead("200", { "Content-Type": "text/html;charset=utf8" });
    // res.end("<h1> Express ์„œ๋ฒ„์—์„œ ์‘๋‹ตํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
});

app.use("/", function (req, res, next) {
    console.log("๋‘ ๋ฒˆ์งธ ๋ฏธ๋“ค์›จ์–ด์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•จ.");

    res.writeHead("200", { "Content-Type": "text/html;charset=utf8" });
    res.end("<h1>Express ์„œ๋ฒ„์—์„œ " + req.user + "๊ฐ€ ์‘๋‹ตํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.</h1>");
});

http.createServer(app).listen(3000, function () {
    console.log("Express ์„œ๋ฒ„๊ฐ€ 3000๋ฒˆ ํฌํŠธ์—์„œ ์‹œ์ž‘๋จ");
});

 

 

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

  • next() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์Œ ๋ฏธ๋“ค์›จ์–ด๋กœ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ๋„˜๊ฒจ์ฃผ์–ด์•ผํ•œ๋‹ค.

// Express ๊ธฐ๋ณธ ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
var express = require("express");

http = require("http");

// ์ต์Šคํ”„๋ ˆ์Šค ๊ฐ์ฒด ์ƒ์„ฑ
var app = express();

app.use(function (req, res, next) {
    console.log("์ฒซ ๋ฒˆ์งธ ๋ฏธ๋“ค์›จ์–ด์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•จ");

    var userAgent = req.header("User-Agent");
    var paramName = req.query.name;

    res.writeHead("200", { "Content-Type": "text/html;charset=utf8" });
    res.write("<h1> Express ์„œ๋ฒ„์—์„œ ์‘๋‹ตํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.</h1>");
    res.write("<div><p>User-Agent :" + userAgent + "</p></div>");
    res.write("<div><p>Param name :" + paramName + "</p></div>");
    res.end();
});


http.createServer(app).listen(3000, function () {
    console.log("Express ์„œ๋ฒ„๊ฐ€ 3000๋ฒˆ ํฌํŠธ์—์„œ ์‹œ์ž‘๋จ");
});

์ต์Šคํ”„๋ ˆ์Šค์˜ ์š”์ฒญ๊ฐ์ฒด์™€ ์‘๋‹ต ๊ฐ์ฒด ์•Œ์•„๋ณด๊ธฐ

  • send([body]) : ํด๋ผ์ด์–ธํŠธ์— ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. (html๋ฌธ์ž์—ด, Buffer ๊ฐ์ฒด , JSON ๊ฐ์ฒด, JSON๋ฐฐ์—ด)
  • status(code) : HTTP ์ƒํƒœ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ์ฝ”๋“œ๋Š” end()๋‚˜ send() ๊ฐ™์€ ์ „์†ก ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€๋กœ ํ˜ธ์ถœํ•ด์•ผ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • sendStatus(statusCode) : HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ ์ฝ”๋“œ๋Š” ์ƒํƒœ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.
  • redirect ([status,]path ) : ์›น ํŽ˜์ด์ง€ ๊ฒฝ๋กœ๋ฅผ ๊ฐ•์ œ๋กœ ์ด๋™ ์‹œํ‚ต๋‹ˆ๋‹ค.
  • render(view [,locals][,callback]) : ๋ทฐ ์—”์ง„์„ ์‚ฌ์šฉํ•ด ๋ฌธ์„œ๋ฅผ ๋งŒ๋“  ํ›„ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

 

์ต์Šคํ”„๋ ˆ์Šค์—์„œ ์š”์ฒญ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•œ ํ—ค๋”์™€ ํŒŒ๋ผ๋ฏธํ„ฐ ์•Œ์•„๋ณด๊ธฐ

  • query : ํด๋ผ์ด์–ธํŠธ์—์„œ get ๋ฐฉ์‹์œผ๋กœ ์ „์†กํ•œ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • body : ํด๋ผ์ด์–ธํŠธ์—์„œ post ๋ฐฉ์‹์œผ๋กœ ์ „์†กํ•œ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • header(name) : ํ—ค๋”๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ •๋ฆฌ

  • ์‹ค๋ฌด์—์„œ๋Š” ํ•˜๋‚˜์— ํŒŒ์ผ์— ๋‹ค ์ •๋ฆฌํ•˜๋ฉด ๊ด€๋ฆฌ๋‚˜ ์œ ์ง€ ๋ณด์ˆ˜ ์–ด๋ ค์›Œ์„œ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๊ฒŒ express ๋ชจ๋“ˆ ์›น ์„œ๋ฒ„
  • ์ธ๋ฐ http ๋ชจ๋“ˆ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค.
  • express ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฏธ๋“ค ์›จ์–ด์™€ ๋ผ์šฐํ„ฐ๋ฅผ ์ด์šฉํ•œ๋‹ค . ์ค‘๊ฐ„ ๋งค๊ฐœ์ฒด ๋Š๋‚Œ์ด๋‹ค.
  • ๋”ฐ๋กœ ๋…๋ฆฝ์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
728x90
๋ฐ˜์‘ํ˜•