nodejs 의 http 소개
2122 단어 nodejs
http 서 비 스 를 만 드 는 것 은 매우 간단 합 니 다.
04_http.js
//04 http
// http
var http=require("http");
// http
var server =http.createServer(function (req,res) {
res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
res.end(" nodejs");
});
// 3000
server.listen(3000);
console.log("nodejs server run at 3000"); 05_http_url_get.js
//05 http url get
// req res 、 url 、 http get
var http=require("http");
var url=require("url");
var server =http.createServer(function (req,res) {
var reqUrl = req.url;
if(reqUrl=='/favicon.ico'){// ico
return ;
}
console.log(reqUrl);
// url req.url urlObj
var urlObj= url.parse(reqUrl,true);
console.log(urlObj);
res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
res.write(' url: '+reqUrl+'
');
res.write('pathname: '+urlObj.pathname+'
'); //requestPage
res.write('get name: '+urlObj.query['name']+'
'); //get
res.write('get age: '+urlObj.query['age']+'
');//get
res.write('search: '+urlObj.search+'
'); // ?name=zhangsan&age=22
// http://localhost:3000/about?name=zhangsan&age=22
//urlObj
// Url {
// protocol: null,
// slashes: null,
// auth: null,
// host: null,
// port: null,
// hostname: null,
// hash: null,
// search: '?name=zhangsan&age=22',
// query: { name: 'zhangsan', age: '22' },
// pathname: '/about',
// path: '/about?name=zhangsan&age=22',
// href: '/about?name=zhangsan&age=22' }
res.end();//
});
server.listen(3000);
console.log('nodejs server run at 3000');