http 모듈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // http 모듈 sever 객체 // 웹 서버 생성과 실행 const http = require('http'); const server = http.createServer(); server.listen(50000, () => { console.log('서버가 동작 중입니다, http://127.0.0.1:50000'); }); // 웹서버 종료 const testClose = function () { server.close(); console.log('서버가 종료되었습니다, http://127.0.0.1:50000'); }; // 강제 서버종료 setTimeout(testClose, 5000); | cs |
event
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 | // http 모듈 sever 객체의 이벤트 // 웹 서버 생성과 실행 const http = require('http'); const server = http.createServer(); // 이벤트 연결 server.on('request', () => { console.log('Request'); }); server.on('connection', () => { console.log('Connection'); }); server.on('close', () => { console.log('Close'); }); server.listen(50000, () => { console.log('서버가 동작 중입니다, http://127.0.0.1:50000'); }); // 웹서버 종료 const testClose = function () { server.close(); console.log('서버가 종료되었습니다, http://127.0.0.1:50000'); }; // 강제 서버종료 setTimeout(testClose, 10000); | cs |
response 객체
1 2 3 4 5 6 | require('http').createServer((request, response) => { response.writeHead(200, { 'Content-Type': 'text/html' }); response.end('Hello World!'); }).listen(50000, () => { console.log('서버가 동작 중입니다, http://127.0.0.1:50000'); }); | cs |
response 객체, fs 모듈 활용 1
1 2 3 4 5 6 7 8 9 10 11 | const fs = require('fs'); const http = require('http'); http.createServer((request, response) => { fs.readFile('./test.html', (error, data) => { response.writeHead(200, { 'Content-Type': 'text/html' }); response.end(data); }); }).listen(50000, () => { console.log('서버가 동작 중입니다, http://127.0.0.1:50000'); }); | cs |
response 객체, fs 모듈 활용 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | const fs = require('fs'); const http = require('http'); http.createServer((request, response) => { fs.readFile('./test.jpg', (error, data) => { response.writeHead(200, { 'Content-Type': 'image/jpeg' }); response.end(data); }); }).listen(50001, () => { //50001 포트 console.log('서버가 동작 중입니다, http://127.0.0.1:50001'); }); http.createServer((request, response) => { fs.readFile('./Cullah_DaftPunk.mp3', (error, data) => { response.writeHead(200, { 'Content-Type': 'audio/mp3' }); response.end(data); }); }).listen(50002, () => { //50002 포트 console.log('서버가 동작 중입니다, http://127.0.0.1:50002'); }); | cs |
타입 |
일반적인 서브타입 예시 |
설명 |
text |
text/plain, text/html, text/css, text/javascript |
텍스트를 포함하는 모든 문서를 나타내며 이론상으로는 인간이 읽을수 있어야한다. |
image |
image/gif, image/png, image/jpeg, image/bmp, image/webp |
모든 종류의 이미지를 나타내며 애니메이션되는 이미지가 이미지 타입에 포함되긴 하지만 비디오는 포함되지 않는다. |
audio |
audio/midi, audio/mpeg, audio/webm, audio/ogg, audio/wav |
모든 종류의 오디오 파일을 나타낸다. |
video |
video/webm, video/ogg |
모든 종류의 비디오 파일을 나타낸다. |
request 객체, url 속성 활용
const fs = require('fs');
const http = require('http');
const url = require('url');
http.createServer((request, response) => {
let pathname = url.parse(request.url);
pathname = url.parse(request.url).pathname;
if (pathname === '/') {
fs.readFile('./138_index.html', (error, data) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(data);
console.log(url.parse(request.url));
});
} else if (pathname === '/example') {
fs.readFile('./136_example.html', (error, data) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(data);
console.log(url.parse(request.url));
});
}
}).listen(50000, () => {
console.log('서버가 동작 중입니다, http://127.0.0.1:50000');
});
request 객체의 속성
url |
요청한 URL 정보 |
headers |
요청 메시지 헤더 정보 |
method |
클라이언트의 요청 방식 |
httpVersion |
HTTP 프로토콜의 버전 |
- GET, POST -
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Example for POST</h1>
<form method = "post"> /*post 방식. get이라 적으면 get방식이다*/
<table>
<tr>
<td>Soju</td>
<td><input type = "text" name = "beverage_1"/></td>
</tr>
<td>Beer</td>
<td><input type = "text" name = "beverage_2"/></td>
</table>
<input type = "submit" />
</form>
</body>
</html>
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
/*http 프로토콜에서 메시지를 요청하는 방법 중 가장 많이 사용하는 GET, POST 방식*/ if (request.method === 'GET') { //GET 요청일 경우. request의 method 속성을 사용하여 구분
fs.readFile('./test.html', (error, data) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(data);
console.log(`${request.method}방식의 요청입니다`);
});
} else if (request.method === 'POST') { //POST 요청일 경우. 여러개의 값을 숨겨서 보낼수있다.
request.on('data', (data) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(data);
console.log(`${request.method}방식의 요청입니다`);
});
}
}).listen(50000, () => {
console.log('서버가 동작 중입니다, http://127.0.0.1:50000');
});
'공부 > Node.js' 카테고리의 다른 글
템플릿 엔진 모듈 - ejs, pug 모듈 (0) | 2019.03.07 |
---|---|
크롤링 - cheerio, iconv-lite 모듈 (1) | 2019.03.07 |
npm (0) | 2019.03.06 |
fs 모듈 (0) | 2019.03.06 |
API 읽는 법 - fs.access(path[,mode],callback) (0) | 2019.03.06 |