http模块
// 1 引入http模块
const http = require(“http”)
// 2 创建http服务对象
// req==reqest请求体 --浏览器–>服务器
/*
req.url 地址 提取地址栏数据
req.on(‘data’) 提取非地址栏数据 所有的https都会触发end事件
req.on('end)
*/
// res==response 回应
/*
res.writeHead(200,{‘Content-type’:“text/html;charset=utf-8”});响应头设置
res.write(字符/数据)返回数据
res.end()结束响应 必须写的
*/
const app = http.createServer((req, res) => {
console.log(“前端访问了”)
res.writeHead(200, { ‘Content-type’: “text/html;charset=utf-8” })
// res.write(“你好”)
res.write(`
// 模板字符串内可以放入整个HTML页面的代码。
res.end()//后端需要结束响应,必须要写的
})
// 3 监听服务器(端口,[地址],[回调函数])
app.listen(3000, “localhost”, () => {
console.log(“server in running…”)
})