Javascript/Node.js

[Node.js] (3) ์š”์ฒญ ๊ฐ์ฒด

Rainbow๐ŸŒˆCoder 2022. 11. 23. 17:52
728x90
const http = require('http');

const hostname = '127.0.0.1';
const port = 1080;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
	res.setHeader('Content-Type', 'text/plain');
	console.log(req);
	res.end('Hello World');
});

server.listen(port, hostname, () => {
	console.log(`Server running at http://${hostname}:${port}/`);
});
	console.log(req);

 

 ์š”์ฒญ ๊ฐ์ฒด

ํ—ค๋”๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ฆ‰, ์š”์ฒญ ๋ฐ ์‘๋‹ต์— ์ถ”๊ฐ€๋œ ๋ฉ”ํƒ€ ์ •๋ณด์ด๋‹ค.
์œ„ ์บก์ฒ˜์™€ ๊ฐ™์ด req ๊ฐ์ฒด๋ฅผ ์ฝ˜์†” ๋กœ๊ทธ์— ์ฐ์œผ๋ฉด
์˜ˆ๋ฅผ ๋“ค์–ด ์‘๋‹ต ๋ฐ์ดํ„ฐ ์บ์‹œ ๋ฐฉ๋ฒ•, ์š”์ฒญ์— ์‚ฌ์šฉ๋˜๋Š” ๋ธŒ๋ผ์šฐ์ €, ์‘๋‹ต(accept)์œ ํ˜•์œผ๋กœ๋Š” html, xml ์‘๋‹ต์„ ์ถ•์†Œํ•ด ๋Œ€์—ญํญ์„ ์•„๋ผ๋Š” ์•”ํ˜ธํ™” ์‘๋‹ต ๋“ฑ์˜ ํ—ค๋”๊ฐ€ ๋‚˜์˜จ๋‹ค.

์ด๋ ‡๋“ฏ ํ•œ ์š”์ฒญ์—์„œ ์ˆ˜๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ ์ค‘...

 

์ค‘์š”ํ•œ ํ•„๋“œ(1) : url

console.log(req.url, req.method, req.headers);

console.log(req.url, req.method, req.headers);
               /        GET     ํ—ค๋” ๊ทธ์ž์ฒด ์ฐํ˜€๋‚˜์˜ด
// ์ฐธ๊ณ ๋กœ URL์„ ๋ธŒ๋ผ์šฐ์ €์— ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ GET ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

url์€ ํ˜ธ์ŠคํŠธ ๋‹ค์Œ์— ๋ถ™๋Š” ๋ชจ๋“  ์ฃผ์†Œ์ธ๋ฐ, http://localhost:3001/

์ด ๊ฒฝ์šฐ localhost ์™ธ์— ์•„๋ฌด ๊ฒƒ๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— / ๋งŒ ๋‚˜์˜จ ๊ฒƒ์ด๋‹ค.

 

์ฃผ์†Œ์ฐฝ์˜ / ๋’ค์— test๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋ฉด..

 

728x90