-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
878 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* express-error-handler 미들웨어로 오류 페이지 보내기 | ||
*/ | ||
|
||
|
||
var express = require('express'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
var bodyParser = require('body-parser'); | ||
var staticPath= require('serve-static'); | ||
var expressErrorHandler = require('express-error-handler'); | ||
|
||
var app = express(); | ||
var router = express.Router(); | ||
|
||
app.set('port', process.env.PORT || 3000); | ||
|
||
// body-parser 를 사용해 application/x-www-from-urlencoded 파싱 | ||
app.use(bodyParser.urlencoded( { extended : false })); | ||
|
||
// body-parser 를 사용해 application/json 파싱 | ||
app.use(bodyParser.json()); | ||
|
||
app.use(staticPath(path.join(__dirname, 'public'))); | ||
|
||
|
||
router.route('/process/#').post(function(req, res){ | ||
console.log('/process/# 처리함'); | ||
|
||
var paramId = req.body.id || req.query.id; | ||
var paramPassword = req.body.password || req.query.password; | ||
|
||
res.writeHead(200, { 'Content-Type' : 'text/html;charset=utf8'}); | ||
res.write('<h1>Express 서버에서 응답한 결과입니다.</h1>'); | ||
res.write('<div><p>Param id : ' + paramId + '</p></div>'); | ||
res.write('<div><p>Param password : ' + paramPassword + '</p></div>'); | ||
res.write('<div><a hrep="/public/#2.html">로그인 페이지로 돌아가기</a></div>'); | ||
res.end(); | ||
}); | ||
|
||
|
||
router.route('/process/users/:id').get(function(req, res){ | ||
console.log('/process/users/:id 처리함'); | ||
|
||
var paramId = req.params.id; | ||
|
||
console.log('/process/users와 토큰 %s를 이용해 처리함', paramId); | ||
|
||
res.writeHead(200, { 'Content-Type' : 'text/html;charset=utf8'}); | ||
res.write('<h1>Express 서버에서 응답한 결과입니다.</h1>'); | ||
res.write('<div><p>Param id : ' + paramId + '</p></div>'); | ||
res.end(); | ||
}); | ||
|
||
|
||
app.use('/', router); | ||
|
||
|
||
// 모든 router 처리 끝난 후 404 오류 페이지 처리 | ||
var errorHandler = expressErrorHandler({ | ||
static : { '404' : './public/404.html' } | ||
}); | ||
|
||
app.use( expressErrorHandler.httpError(404) ); | ||
app.use( errorHandler ); | ||
|
||
http.createServer(app).listen(3000, function(){ | ||
console.log('starting Express Server at 3000 port'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* 쿠키처리하기 | ||
*/ | ||
|
||
|
||
var express = require('express'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
var bodyParser = require('body-parser'); | ||
var staticPath= require('serve-static'); | ||
var expressErrorHandler = require('express-error-handler'); | ||
var cookieParser = require('cookie-parser'); | ||
|
||
var app = express(); | ||
|
||
app.use(cookieParser()); | ||
|
||
var router = express.Router(); | ||
|
||
app.set('port', process.env.PORT || 3000); | ||
|
||
app.use(bodyParser.urlencoded( { extended : false })); | ||
app.use(bodyParser.json()); | ||
|
||
app.use(staticPath(path.join(__dirname, 'public'))); | ||
|
||
|
||
|
||
|
||
router.route('/process/#').post(function(req, res){ | ||
console.log('/process/# 처리함'); | ||
|
||
var paramId = req.body.id || req.query.id; | ||
var paramPassword = req.body.password || req.query.password; | ||
|
||
res.writeHead(200, { 'Content-Type' : 'text/html;charset=utf8'}); | ||
res.write('<h1>Express 서버에서 응답한 결과입니다.</h1>'); | ||
res.write('<div><p>Param id : ' + paramId + '</p></div>'); | ||
res.write('<div><p>Param password : ' + paramPassword + '</p></div>'); | ||
res.write('<div><a hrep="/public/#2.html">로그인 페이지로 돌아가기</a></div>'); | ||
res.end(); | ||
}); | ||
|
||
|
||
router.route('/process/users/:id').get(function(req, res){ | ||
console.log('/process/users/:id 처리함'); | ||
|
||
var paramId = req.params.id; | ||
|
||
console.log('/process/users와 토큰 %s를 이용해 처리함', paramId); | ||
|
||
res.writeHead(200, { 'Content-Type' : 'text/html;charset=utf8'}); | ||
res.write('<h1>Express 서버에서 응답한 결과입니다.</h1>'); | ||
res.write('<div><p>Param id : ' + paramId + '</p></div>'); | ||
res.end(); | ||
}); | ||
|
||
|
||
|
||
router.route('/process/showCookie').get(function(req, res){ | ||
console.log('/process/showCookie 호출됨'); | ||
|
||
res.send(req.cookies); | ||
}); | ||
|
||
router.route('/process/setUserCookie').get(function(req, res){ | ||
console.log('/process/setUserCookie 호출됨'); | ||
|
||
res.cookie('user', { | ||
id : 'mike', | ||
name : '소녀시대', | ||
authorized : true | ||
}); | ||
|
||
res.redirect('/process/showCookie'); | ||
}); | ||
|
||
|
||
|
||
app.use('/', router); | ||
|
||
|
||
// 모든 router 처리 끝난 후 404 오류 페이지 처리 | ||
var errorHandler = expressErrorHandler({ | ||
static : { '404' : './public/404.html' } | ||
}); | ||
|
||
app.use( expressErrorHandler.httpError(404) ); | ||
app.use( errorHandler ); | ||
|
||
http.createServer(app).listen(3000, function(){ | ||
console.log('starting Express Server at 3000 port'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* 세션처리하기 | ||
*/ | ||
|
||
|
||
var express = require('express'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
var bodyParser = require('body-parser'); | ||
var staticPath= require('serve-static'); | ||
var expressErrorHandler = require('express-error-handler'); | ||
var cookieParser = require('cookie-parser'); | ||
var expressSession = require('express-session'); | ||
|
||
var app = express(); | ||
|
||
app.use(cookieParser()); | ||
app.use(expressSession({ | ||
secret : 'my key', | ||
resave : true, | ||
saveUninitialized : true | ||
})); | ||
|
||
|
||
|
||
var router = express.Router(); | ||
|
||
app.set('port', process.env.PORT || 3000); | ||
|
||
app.use(bodyParser.urlencoded( { extended : false })); | ||
app.use(bodyParser.json()); | ||
|
||
app.use(staticPath(path.join(__dirname, 'public'))); | ||
|
||
|
||
|
||
router.route('/process/product').get(function(req, res){ | ||
console.log('/process/product 호출됨'); | ||
|
||
if(req.session.user){ | ||
res.redirect('/product.html'); | ||
}else{ | ||
res.redirect('/#2.html'); | ||
} | ||
}); | ||
|
||
|
||
router.route('/process/#').post(function(req, res){ | ||
console.log('/process/# 호출됨'); | ||
|
||
var paramId = req.body.id || req.query.id; | ||
var paramPassword = req.body.password || req.query.password; | ||
|
||
if(req.session.user){ | ||
console.log('이미 로그인 되셨습니다.'); | ||
|
||
res.redirect('/product.html'); | ||
}else{ | ||
req.session.user = { | ||
id : paramId, | ||
name : '소녀시돼', | ||
authorized : true | ||
}; | ||
|
||
res.writeHead('200', { 'Content-Type' : 'text/html;charset=utf8' }); | ||
res.write('<h1>로그인 성공</h1>'); | ||
res.write('<div><p>Param id : ' + paramId + '</p></div>'); | ||
res.write('<div><p>Param password : ' + paramPassword + '</p></div>'); | ||
res.write('<br/><br/><a href="/process/product">로그인후 페이지로 이동</a>'); | ||
res.end(); | ||
} | ||
}); | ||
|
||
router.route('/process/logout').get(function(req, res){ | ||
console.log('/process/logout 호출됨'); | ||
|
||
if(req.session.user){ | ||
console.log('로그아웃합니다'); | ||
|
||
req.session.destroy(function(err){ | ||
if(err) { throw err; } | ||
|
||
console.log('세션을 삭제하고 로그아웃되었습니다.'); | ||
res.redirect('/#2.html'); | ||
}); | ||
}else{ | ||
console.log('로그인되지 않았습니다.'); | ||
|
||
res.redirect('/#2.html'); | ||
} | ||
}); | ||
|
||
|
||
|
||
app.use('/', router); | ||
|
||
|
||
// 모든 router 처리 끝난 후 404 오류 페이지 처리 | ||
var errorHandler = expressErrorHandler({ | ||
static : { '404' : './public/404.html' } | ||
}); | ||
|
||
app.use( expressErrorHandler.httpError(404) ); | ||
app.use( errorHandler ); | ||
|
||
http.createServer(app).listen(3000, function(){ | ||
console.log('starting Express Server at 3000 port'); | ||
}); |
Oops, something went wrong.