Mingyu

Express server

var express = require('express')
, http = require('http')
, path = require('path');
var bodyParser = require('body-parser')
, static = require('serve-static');
var expressErrorHandler = require('express-error-handler');
var app = express();
//라우터 객체 참조
var router = express.Router();
//라우팅 함수 등록
router.route('/process/login/:name').post(function (req, res) {
console.log('/process/login/:name 처리함');
var paramName = req.params.name;
var paramId = req.body.id || req.query.id;
var paramPassword = req.body.password || req.query.password;
//GET, POST 모두 고려해서 둘 다 검사
res.writeHead('200', { 'Content-Type': 'text/html;charset=utf8' });
res.write('<h1>Result form Express Server</h1>');
res.write('<div><p>Param name : ' + paramName + '</p></div>');
res.write('<div><p>Param id : ' + paramId + '</p></div>');
res.write('<div><p>Param password : ' + paramPassword + '</p></div>');
res.write("<br><br><a href ='/login3.html'>로그인 페이지로 돌아가기</a>");
res.end();
});
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(static(path.join(__dirname, 'public')));
app.use('/', router);
//모든 router 처리가 끝난 후 404 오류 페이지 처리
var errorHandler = expressErrorHandler({
static: {
'404': 'C:/Users/mingyu/Desktop/git/rest_stop_list/app/public/404.html'
}
});
app.use(expressErrorHandler.httpError(404));
app.use(errorHandler);
http.createServer(app).listen(app.get('port'), function () {
console.log('익스프레스 서버를 시작했습니다.');
});
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<tilte>오류 페이지</tilte>
</head>
<body>
<h3>ERROR - 페이지를 찾을 수 없습니다.</h3>
<hr/>
<p>/public/404.html 파일의 오류 페이지</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<tilte>로그인 테스트</tilte>
</head>
<body>
<h1>로그인</h1>
<br>
<form method="post" action="/process/login/rest_list">
<table>
<tr>
<td><label>아이디</label></td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><label>비밀번호</label></td>
<td><input type="text" name="password"></td>
</tr>
</table>
<input type="submit" value="전송" name="">
</form>
</body>
</html>
\ No newline at end of file
This diff is collapsed. Click to expand it.
{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"express": "^4.18.1",
"express-error-handler": "^1.1.0",
"http": "^0.0.1-security",
"path": "^0.12.7",
"serve-static": "^1.15.0"
}
}