패키지 json 파일을 만든다.
# npm init
-대문자 안 된다.
# express
package.json 이름 충돌 주의
#npm install express --save
app8.js
var express=require('express'); var http=require('http'); var static=require('serve-static'); var path=require('path'); var bodyParser=require('body-parser'); var app=express(); app.set('port', process.env.PORT || 3000); app.use('/public', static(path.join(__dirname, 'public'))); app.use(bodyParser.urlencoded({extend:false})); app.use(bodyParser.json()); 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>서버에서 로그인 응답</h1>"); res.write("<div><p>"+paramId+"</p></div>"); res.write("<div><p>"+paramPassword+"</p></div>"); res.end(); }); //모든 요청에 대한 처리 app.all('*', function(req, res){ res.status(404).send('<h1>요청하신 페이지는 없어요. </h1>'); }); app.use('/', router); var server=http.createServer(app).listen(app.get('port'), function(){ console.log('익스프레스로 웹 서버를 실행함 : ' + app.get('port')) ; });
app8_02.js
var express=require('express'); var http=require('http'); var static=require('serve-static'); var path=require('path'); var bodyParser=require('body-parser'); var app=express(); app.set('port', process.env.PORT || 3000); app.use('/public', static(path.join(__dirname, 'public'))); app.use(bodyParser.urlencoded({extend:false})); app.use(bodyParser.json()); 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; res.writeHead(200, {"Content-Type":"text/html;charset=utf8"}); res.write("<h1>서버에서 로그인 응답</h1>"); res.write("<div><p>"+paramName+"</p></div>"); res.write("<div><p>"+paramId+"</p></div>"); res.write("<div><p>"+paramPassword+"</p></div>"); res.end(); }); app.use('/', router); var server=http.createServer(app).listen(app.get('port'), function(){ console.log('익스프레스로 웹 서버를 실행함 : ' + app.get('port')) ; });
index2.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>로그인</title> </head> <body> <h1>로그인</h1> <br> <form method="post" action="/process/login"> <table> <tr> <td><label>아이디</label></td> <td><input type="text" name="id"></td> </tr> <tr> <td><label>비밀번호</label></td> <td><input type="password" name="password"></td> </tr> </table> <input type="submit" value="전송" name=""> </form> </body> </html>
index3.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>로그인</title> </head> <body> <h1>로그인</h1> <br> <form method="post" action="/process/login/mike"> <table> <tr> <td><label>아이디</label></td> <td><input type="text" name="id"></td> </tr> <tr> <td><label>비밀번호</label></td> <td><input type="password" name="password"></td> </tr> </table> <input type="submit" value="전송" name=""> </form> </body> </html>
https://github.com/braverokmc79/nodejs_webproject1
댓글 ( 4)
댓글 남기기