Nodejs新手笔记
总结一下知识点
Http - 1
var api="http://www.baidu.com?name=zhangsan&age=20";
var urlparse = url.parse(api, true); //启用了
var newUrl = new URL(api); // 新的方法解析URL
console.log(urlparse);
console.log(newUrl);
console.log(urlparse.query);
console.log(newUrl.searchParams);
console.log(newUrl.searchParams.values());
console.log(`name:${urlparse.query.name}, age:${urlparse.query.age}` );
console.log(`name:${newUrl.searchParams.get("name")}, age:${newUrl.searchParams.get("age")}` );
//descructuring
const {name, age} = urlparse.query;
console.log(`name:${name}, age:${age}` );
Http - 2
让局域网其他用户访问你的服务
http.createServer(function(req, res) {
console.log(req.url);
res.writeHead(200, { "Content-Type":"text/html;charset='utf-8' "});
res.write('<head><meta charset="UTF-8"></head>') //html也要设置编码,不然中文会乱码
res.write('this is nodejs');
res.write('<h2>你好 nodejs</h2>');
res.end();
}).listen(2645, '0.0.0.0'); //让局域网其他用户访问要用0.0.0.0而不是127.0.0.1