-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
34 lines (28 loc) · 1.13 KB
/
app.js
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
32
33
/** * 웹 서버에 html 파일 서비스 하기 */
var http = require('http');
var fs = require('fs'); // 파일 읽기, 쓰기 등 을 할 수 있는 모듈
// 404 error message : 페이지 오류가 발생했을때,
function send404Message(response){
response.writeHead(404,{"Content-Type":"text/plain"}); // 단순한 글자 출력
response.write("404 ERROR... ");
response.end();
}
// 200 Okay : 정상적인 요청
function onRequest(request, response){
console.log(request.url);
//response.writeHead(200,{"Content-Type":"text/html"}); // 웹페이지 출력
if(request.method == 'GET' && request.url == '/'){
response.writeHead(200,{"Content-Type":"text/html"}); // 웹페이지 출력
fs.createReadStream("./index.html").pipe(response); // 같은 디렉토리에 있는 index.html를 response 함
} else {
var path = "." + request.url;
if (fs.existsSync(path)) {
fs.createReadStream(path).pipe(response);
} else {
// file이 존재 하지않을때,
send404Message(response);
}
}
}
http.createServer(onRequest).listen(8080);
console.log("Server Created...");