//引入http模块
const http = require("http");
//引入chalk模块 这是一个作用在vs code上的颜色工具
const chalk = require("chalk");
//引入 配置的变量
const config=require("./config/defaultConfig");
//实例话化服务, req 是客户端的请求,包含各种信息; res是 返回到客户端的内容;
const server = http.createServer( (req,res) => {
// 输出的 状态为200;
res.statusCode=200;
//设置头为 编码方式 为 text/plain: 数据以纯文本形式(text/json/xml/html)进行编码,其中不含任何控件或格式字符。
res.setHeader("Content-Type","text/html")
//书写流
res.write("<html>");
res.write("<body>");
res.write("<div style='color:red'>");
res.write("heo11ne");
res.write("</div>");
res.write("</body>");
//流书写一定要用end结束
res.end("</html>");
})
//监听 3030 端口
server.listen(3030,() => {
// 定义输出内容
const addr = `http://${config.hosts}:${config.ports}`
// 利用 打印台输出有颜色的内容
console.log(`server is running in ${chalk.blue(addr)}`)
})
除了nodemon 以外还有一种 热更新方法;
全局安装 supervisor
sudo npm i -g supervisor
这边就可以使用supervisor 来全局 watch 文件
很简单 ‘supervisor + 文件’ 就可以启动了