Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 게임
- promise
- js
- execCommand
- async
- json
- ajax
- prj
- addEventListener
- callback
- https://m.blog.naver.com/tt2t2am1118/221010125300
- Project
- object
- mysql
- Import
- 비동기
- sql
- setTimeout()
- https://youtube.com/playlist?list=PLuHgQVnccGMA5836CvWfieEQy0T0ov6Jh&si=FTaYv8m21EhO-A2K
- eport
- database
- 혼프
- JS #프로젝트
- Porject
- db
- slow and steady
- 동기
- 참고블로그
- await
- webpack
Archives
- Today
- Total
C-log
📗Nodejs : section19 - NotFound처리와 홈페이지 제작구현 본문
728x90
예외적인 url경로를 들어 갔을 때 NotFound라고 표기가 되는 페이지를 생성하려고 한다. 그렇게 어렵지 않으니 필요한 코드만 살펴보자.
if (pathname === '/') {
fs.readFile(`data/${queryData.id}`, 'utf8', function (err, description) {
var template = `
<!doctype html>
<html>
<head>
...
<p>${description}</p>
</body>
</html>
`;
response.writeHead(200);
response.end(template);
});
} else {
response.writeHead(404);
response.end('Not found');
}
우리가 pathname으로 undefind가 되는 영역이 이후 즉, 특정 pathname을 부여하지 않는 url구문에서는 NotFound라는 구문이 랜더링되는 부분이다. 그리고 홈 영역은 앞서 말했 듯이 undefind부분이다. 그래서 우리는 아래와 같이 수정해줄 필요가 있다. 아래와 같이 작성하면 된다.
...
if (pathname === '/') {
if (queryData.id === undefined) { //id가 없을 때
fs.readFile(`data/${queryData.id}`, 'utf8', function (err, description) {
var title = 'Welcome'; //id가 없을 때 Welcome
var description = 'Hello, Node.js';
var template = `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
...
이렇게해서 우리가 페이지를 url을 포함해서 조금 더 구체적으로 만들어 냈다.
728x90
'📗Nodejs > ⚡ver.0' 카테고리의 다른 글
📗Nodejs : section26 - 함수를 이용해서 정리 정돈하기 (0) | 2024.06.17 |
---|---|
📗Nodejs : section24 - readdir으로 파일목록 만들기 (0) | 2024.01.11 |
📗Nodejs : section13 - readFile (0) | 2024.01.09 |
📗Nodejs : section11 - URL, query string-2 (0) | 2024.01.09 |
📗Nodejs : section10 - URL, query string (0) | 2024.01.08 |
Comments