Node js

node js url로 페이지 이동구분 주기

이상욱1 2015. 3. 25. 12:37

아래소스는 url 로 요청을 각각 구분 주어서 서비스 할 수 있다 



var http= require('http');

var fs=require('fs');

var url=require('url');


http.createServer(function(request,response){

var pathname=url.parse(request.url).pathname;

//페이지 구분 

if(pathname =='/'){

fs.readFile('index.html',function(error , data){

response.writeHead(200 , {'Content-Type': 'text/html'});

response.end(data);

});

}else if(pathname == '/otherpage'){

/*console.log('/other here');*/

fs.readFile('otherpage.html',function(error , data){

response.writeHead(200 , {'Content-Type': 'text/html'});

response.end(data);

});

}

}).listen(52275 ,function(){

console.log('runnig server at http://127.0.0.1:52275');

});


html 파일을 두개 만들어 놓고 조건에 따라서 해당 서비스를 해주는 화면 

결과 값은 아래와 같다