- 导入http模块
const http=require(‘http’); - 创建web服务
var server =http.createServer(); - 请求,处理,响应
server.on(‘request’,function(req,res){
console.log(req);
res.end(“我是响应”);
}); - 启动web服务,监听浏览器的请求
server.listen(3000,’127.0.0.1’,function(err){
if (err) {
console.log(err);
}
console.log(“success”);
});
- 模块在使用前要引入
const url=require(‘url’) - url模块可以将请求的地址转换为对象
url.parse(req.url) - 模板字符串: 内容写在
之间
- 用法:可以直接写内容
var str=我是内容
- 里面可以直接加变量
var name=’zs’;
var str1=’你好,${name}’;
- 用法:可以直接写内容
- node.js跟js函数定义的区别
js: function func(x,y){}
node.js: const func=(x,y)=>{}
- 模块在使用前要引入