728x90
๋ฐ์ํ
์๋ก
middle์จ์ด์ ๋ผ์ํ ์ ์์๋ณด์
์์ฒญ url์ ์ผ์ผ์ด ํ์ธํด์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ด ๋ผ์ฐํฐ ๋ฏธ๋ค์จ์ด์ ๋๋ค.
๋ผ์ฐํฐ ๋ฏธ๋ค์จ์ด ์ฌ์ฉํ๊ธฐ
๋ผ์ฐํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ์ฉ์๊ฐ ์์ฒญํ ๊ธฐ๋ฅ์ด ๋ฌด์์ธ์ง ํจ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ณํ๊ธฐ ๋๋ฌธ์ ์์ฃผ ์ค์ํฉ๋๋ค.
* Router ๊ฐ์ฒด๋ฅผ ์ด์ฉํด ๋ผ์ฐํ
ํจ์ ๋ฑ๋กํ๊ธฐ
*
* (1) ์น ๋ธ๋ผ์ฐ์ ์์ http://localhost:3000/public/login2.html ํ์ด์ง ์ด๊ณ ์์ฒญ
* (2) ํฌ๋กฌ ๋ธ๋ผ์ฐ์ ์ Postman ์ฑ์ด๋ ๊ธฐํ POST ์์ฒญ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ POST ๋ฐฉ์์ผ๋ก ์์ฒญ
*
* @date 2016-10-25
* @author Mike
*/
// Express ๊ธฐ๋ณธ ๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
var express = require('express')
, http = require('http')
, path = require('path');
// Express์ ๋ฏธ๋ค์จ์ด ๋ถ๋ฌ์ค๊ธฐ
var bodyParser = require('body-parser')
, static = require('serve-static');
// ์ต์คํ๋ ์ค ๊ฐ์ฒด ์์ฑ
var app = express();
// ๊ธฐ๋ณธ ์์ฑ ์ค์
app.set('port', process.env.PORT || 3000);
// body-parser๋ฅผ ์ด์ฉํด application/x-www-form-urlencoded ํ์ฑ
app.use(bodyParser.urlencoded({ extended: false }))
// body-parser๋ฅผ ์ด์ฉํด application/json ํ์ฑ
app.use(bodyParser.json())
app.use('/public', static(path.join(__dirname, 'public')));
// ๋ผ์ฐํฐ ๊ฐ์ฒด ์ฐธ์กฐ
var router = express.Router();
// ๋ผ์ฐํ
ํจ์ ๋ฑ๋ก
router.route('/process/login').post(function(req, res) {
console.log('/process/login ์ฒ๋ฆฌํจ.');
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("<br><br><a href='/public/login2.html'>๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋์๊ฐ๊ธฐ</a>");
res.end();
});
// ๋ผ์ฐํฐ ๊ฐ์ฒด๋ฅผ app ๊ฐ์ฒด์ ๋ฑ๋ก
app.use('/', router);
// ๋ฑ๋ก๋์ง ์์ ํจ์ค์ ๋ํด ํ์ด์ง ์ค๋ฅ ์๋ต
app.all('*', function(req, res) {
res.status(404).send('<h1>ERROR - ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.</h1>');
});
// Express ์๋ฒ ์์
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
728x90
๋ฐ์ํ
'๐ Study > JS, JQuery' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[node.js] req.params vs req.body vs req.query (1) | 2022.09.20 |
---|---|
Mongo DB์ Node.js ์ฐ๋ํ๊ธฐ (0) | 2022.05.15 |
๋ฏธ๋ค์จ์ด ์ฌ์ฉํ๊ธฐ (0) | 2022.05.14 |
์ต์คํ๋ ์ค๋ก ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2022.05.14 |
๊ฐ๋จํ ์น์๋ฒ ๋ง๋ค๊ธฐ (0) | 2022.05.08 |