1.HTTP模块
const http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {"Content-Type": "text/plain;charset=utf-8"});
response.write("hello world");
response.end();
}).listen(9999);
const http = require('http');
http.createServer(function(request, response){
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.statusCode = 200;
response.statusMessage = "OK-200";
response.write("hello 世界");
response.end();
}).listen(9999);
2.URL模块
> var url = require("url")
> urlObj = url.parse("http://www.baidu.com?kw=nodejs")
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: null,
search: '?kw=nodejs',
query: 'kw=nodejs',
pathname: '/',
path: '/?kw=nodejs',
href: 'http://www.baidu.com/?kw=nodejs'
}
> urlObj = url.parse("http://www.baidu.com?kw=nodejs", true)
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: null,
search: '?kw=nodejs',
query: [Object: null prototype] { kw: 'nodejs' },
pathname: '/',
path: '/?kw=nodejs',
href: 'http://www.baidu.com/?kw=nodejs'
}
> urlObj.query.kw
'nodejs'