Created
January 20, 2020 12:06
-
-
Save Odomhae/bc54c9144c1c483b1738970def6b0772 to your computer and use it in GitHub Desktop.
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
var express = require('express'), | |
http = require('http'); | |
var app = express(); | |
/* localhost:3000에 root글자를 띄운다 | |
// 라우팅 // https://expressjs.com/ko/guide/routing.html | |
// get() 메소드로 라우터를 등록 | |
app.get('/',function(req,res){ | |
res.send('root'); | |
}) */ | |
/* localhost:3000에 wwww굴자를 띄운다*/ | |
// 미들웨어 // https://expressjs.com/ko/guide/using-middleware.html | |
// use()메소드로 미들웨어를 설정한다. | |
// 미들웨어를 사용하기 전에 use() 메소드를 호출해야한다. | |
// app.use 안에 있는 함수들이 미들웨어이다 | |
// 요청이 올때마다 미들웨어를 통해 클라이언트에게 응답한다. | |
app.use(function(req,res,next){ | |
console.log('1st 미들웨어에서 요청을 처리함'); | |
//res.send('wwwww'); | |
//res.writeHead('200', {'Content-Type':'text/html;charset=utf8'}); | |
//res.end('<h1>eeeee</h1>'); | |
req.user = 'odom'; | |
next(); | |
}); | |
// '/'없어도 됨 | |
// next 없어도 됨 | |
app.use('/', function(req,res){ | |
console.log('2nd 미들웨어에서 요청을 처리함'); | |
// res.send({name:'tt',age:201}); //for JSON | |
//res.sendStatus(404);// .send('<h1>Forbidden</h1>'); | |
var userAgent = req.header('User-Agent'); | |
var paramName = req.query.name; | |
res.writeHead('200', {'Content-Type':'text/html;charset=utf8'}); | |
res.write('<div><p>User-Agent : '+userAgent + '</p></div>'); | |
res.write('<div><p>Param name : '+paramName + '</p></div>'); | |
res.write('<h1>users name : '+req.user+ '</h1>'); | |
res.end(); | |
}) | |
app.listen(3000, function(){ | |
console.log('Server started'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment