You can make anything
by writing

C.S.Lewis

by 훈오빵 Nov 21. 2023

Node.js 기본서버 만들기(2)

1. 온라인 카지노 게임;OK온라인 카지노 게임;를 반환하는 서버 만들기

const http = require("http");
const server = http.createServer( (req, res) = {
res.setHeader(온라인 카지노 게임;Content-Type온라인 카지노 게임;, 온라인 카지노 게임;text/html온라인 카지노 게임;);
res.end(온라인 카지노 게임;OK온라인 카지노 게임;);
});

server.listen(온라인 카지노 게임;3000온라인 카지노 게임;, () = console.log(온라인 카지노 게임;OK server started!온라인 카지노 게임;));


2. router 만들기

- routing : URL path에 따라 다른 응답을 주는 기능

- URL 형식 : "https://company.com:80/aboutus/#author"

https : 프로토콜 protocol

company.com 호스트명 hostname

:80 포트 port

aboutus 경로 pathname

#author 해시 hash, 일종의 북마크로 서버에 전송하지 않음

const http = require("http");
const url = require(온라인 카지노 게임;url온라인 카지노 게임;);
http
.createServer( (req, res) = {
const path = url.parse(req.url, true).pathname;
res.setHeader(온라인 카지노 게임;Content-Type온라인 카지노 게임;, 온라인 카지노 게임;text/html온라인 카지노 게임;);

if (path === 온라인 카지노 게임;/user온라인 카지노 게임;) {
res.end(온라인 카지노 게임;[user] name : andy, age: 30온라인 카지노 게임;);
} else if (path === 온라인 카지노 게임;/feed온라인 카지노 게임;) {
res.end(`<ul
<lipicture1</li
<lipicture2</li
<lipicture3</li
</ul
`);
} else {
res.statusCode = 404;
res.end(온라인 카지노 게임;404 page not found온라인 카지노 게임;);
}
}).listen("3000", () = console.log("router is ready!"));


3. createServer() refactoring

- 현재 createServer() 안에서 모든 요청에 대한 응답을 컨트롤 하고 있음. createServer() 안에 모든 콜백 함수에 대한 코드를 추가해야 하므로 좋지 않음. 라우팅 이후의 처리를 별도의 함수를 만들어서 처리하도록 리팩토링 해보기.

const http = require("http");
const url = require(온라인 카지노 게임;url온라인 카지노 게임;);
http
.createServer( (req,res) = {
const path = url.parse(req.url, true).pathname;
res.setHeaders(온라인 카지노 게임;Content-Type온라인 카지노 게임;, 온라인 카지노 게임;text/html온라인 카지노 게임;, 온라인 카지노 게임;charset=utf-8온라인 카지노 게임;);

if (path === 온라인 카지노 게임;/user온라인 카지노 게임;) {
user(req, res);
} else if (path === 온라인 카지노 게임;/feed온라인 카지노 게임;) {
feed(req, res);
} else {
notFound(req, res);
}
})
.listen(온라인 카지노 게임;3000온라인 카지노 게임;, () = console.log(온라인 카지노 게임;router is ready!));

const user = (req, res) = {
res.end(`[user] name : andy, age: 30`);
};

const feed = (req, res) = {
res.end(`<ul
<lipicture1</li
<lipicture2</li
<lipicture3</li
</ul
`);
};

const notFound = (req, res) = {
res.statusCode = 404;
res.end(온라인 카지노 게임;404 Page Not Found온라인 카지노 게임;);
};


4. 동적으로 응답하기

- user() 함수를 수정해 매개변수에 따라 동적으로 응답하도록 변경

const user = (req, res) = {
const userInfo = url.parse(req.url, true).query;
res.end(`[user] name: ${userInfo.name}, age: ${userInfo.age}`);
};


5. router refactoring

const http = requrie("http");
const url = require(온라인 카지노 게임;url온라인 카지노 게임;);

http
.createServer( (req, res) = {
const path = url.parse(req.url, true).pathname;
res.setHeader(온라인 카지노 게임;Content-Type온라인 카지노 게임;, 온라인 카지노 게임;text/html온라인 카지노 게임;, 온라인 카지노 게임;charset=utf-8온라인 카지노 게임;);
if (path in urlMap) {
urlMap[path](req, res);
} else {
notFound(req, res);
}
})
.listen(온라인 카지노 게임;3000온라인 카지노 게임;, () = console.log(온라인 카지노 게임;router refactoring!));

const user = (req, res) = {
res.end(`[user] name : andy, age: 30`);
};

const feed = (req, res) = {
res.end(`<ul
<lipicture1</li
<lipicture2</li
<lipicture3</li
</ul
`);
};

const notFound = (req, res) = {
res.statusCode = 404;
res.end(온라인 카지노 게임;404 Page Not Found온라인 카지노 게임;);
};

const urlMap = {
온라인 카지노 게임;/온라인 카지노 게임;: (req, res) = res.end(온라인 카지노 게임;HOME온라인 카지노 게임;),
온라인 카지노 게임;user온라인 카지노 게임;: user,
온라인 카지노 게임;feed온라인 카지노 게임;: feed,
};


브런치는 최신 브라우저에 최적화 되어있습니다.