๐Ÿ“š Study/JS, JQuery

๊ฐ„๋‹จํ•œ ์›น์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ

kkh1902 2022. 5. 8. 00:00
728x90
๋ฐ˜์‘ํ˜•

์„œ๋ก 

node.js๋ฅผ ํ†ตํ•ด์„œ ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•ด์„œ DB์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” appํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๊ธฐ์œ„ํ•ด์„œ ์„œ๋ฒ„ ๊ณต๋ถ€์ค‘์— ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์„œ๋ฒ„ ๊ตฌ์ถ•์„ ํ•ด๋ณด์•˜๋‹ค.

 

๋ณธ๋ฌธ

๋…ธ๋“œ์— ๊ธฐ๋ณธ์œผ๋กœ ๋“ค์–ด ์žˆ๋Š” http๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์›น์„œ๋ฒ„ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•˜๋Š” ์„œ๋ฒ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

http ๋ชจ๋“ˆ์„ ๋กœ๋”ฉํ–ˆ์„ ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด์—๋Š” createServer()๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์˜ ๋˜์–ด์žˆ๋‹ค.

์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์„œ๋ฒ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

//http ๋ชจ๋“ˆ์— ๋“ค์–ด์žˆ๋Š” ์„œ๋ฒ„ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € require()๋ฉ”์†Œ๋“œ๋กœ http ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
var http = require("http");

// ์›น ์„œ๋ฒ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
var server = http.createServer();

// ์›น ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•˜์—ฌ 5000๋ฒˆ ํฌํŠธ์—์„œ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค.
var port = 5000;
server.listen(port, function () {
    console.log("์›น ์„œ๋ฒ„๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. : %d", port);
});

//ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
server.on("connection", function (socket) {
    var addr = socket.address();
    console.log("ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘์†ํ–ˆ์Šต๋‹ˆ๋‹ค. : %s, %d", addr.address, addr.port);
});

//ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ด๋ฒค์ธ  ์ฒ˜๋ฆฌ
server.on("request", function (req, res) {
    console.log("ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.");
    console.dir(req);
});

//์„œ๋ฒ„ ์ข…๋ฃŒ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
server.on("close", function () {
    console.log("์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.");
});

์„œ๋ฒ„ ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ

  • listen(port, [hostname], [backlog], [callback]) : ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋Œ€๊ธฐ์‹œํ‚ต๋‹ˆ๋‹ค.
  • close([callback]) : ์„œ๋ฒ„๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›น์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒค์ธ  ์ฒ˜๋ฆฌํ•˜๊ธฐ

  • connection : ์—ฐ๊ฒฐ์ด ๋งŒ๋“ค์–ด์งˆ๋•Œ ์ด๋ฒคํŠธ
  • request : ์š”์ฒญํ• ๋–„
  • close : ์„œ๋ฒ„ ์ข…๋ฃŒํ•  ๋•Œ

์„œ๋ฒ„์—์„œ ๋‹ค๋ฅธ ์›น ์‚ฌ์ดํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ์‘๋‹ตํ•˜๊ธฐ

http ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด get๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋Š” ์ฝ”๋“œ

var http = require("http");

var options = {
    host: "www.google.com",
    port: 80,
    path: "/",
};

var req = http.get(options, function (res) {
    //์‘๋‹ต์ฒ˜๋ฆฌ
    var resData = "";
    res.on("data", function (chunk) {
        resData += chunk;
    });

    res.on("end", function () {
        console.log(resData);
    });
});

req.on("error", function (err) {
    console.log("์˜ค๋ฅ˜ ๋ฐœ์ƒ: " + err.message);
});

  • http ๊ฐ์ฒด์˜ get() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • get() ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด
    • ๋‘๋ฒˆ์งธ : ์ฝœ๋ฐฑ ํ•จ์ˆ˜ . ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ๋•Œ๋Š” data, end ์ด๋ฒคํŠธ

POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•  ๊ฒฝ์šฐ request() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • ์š”์ฒญ์„ ๋ณด๋‚ด๋ ค๋ฉด ์š”์ฒญ ํ—ค๋”์™€ ๋ณธ๋ฌธ์„ ๋ชจ๋‘ ์ง์ ‘ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
var http = require("http");

var options = {
    host: "www.google.com",
    port: 80,
    method: "POST",
    path: "/",
    headers: {},
};

var req = http.get(options, function (res) {
    //์‘๋‹ต์ฒ˜๋ฆฌ
    var resData = "";
    res.on("data", function (chunk) {
        resData += chunk;
    });

    res.on("end", function () {
        console.log(resData);
    });
});

req.on("error", function (err) {
    console.log("์˜ค๋ฅ˜ ๋ฐœ์ƒ: " + err.message);
});

options.headers["Content-Type"] = "application/x-www-form-urlencoded";
req.data = "q=actor";
options.headers["Content-Length"] = req.data.length;

//์š”์ฒญ ์ „์†ก
req.write(req.data);
req.end();

 

์ •๋ฆฌ

  • ์„œ๋ฒ„๋งŒ๋“œ๋Š” ๊ฑฐ๋Š” http๋ชจ๋“ˆ์„ ์ด์šฉํ•ด์„œ ์„œ๋ฒ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์ž‡๋‹ค. ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ๊ฒŒ require (import)๊ฐœ๋…
  • ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์ด๋ฒคํŠธ (request)์š”์ฒญํ•ด์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ž‘์„ฑํ•œ๋‹ค. 
  • ๋‹ค๋ฅธ ์›น์„œ๋ฒ„ํ•˜๊ณ ๋Š” get, post ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•