使用Node.js原生代码实现静态服务器
const http = require( ‘http’ )//获取nodejs里的http模块
const port = 3000 //设置端口(3000-5000-8000-9000)
const hostname = ‘localhost’ //设置主机名,或者127.0.0.1
http.createServer((request,response) => {
response.writeHead( 200, {//就绪状态码设置为200
‘Content-Type’: ‘text/html;charset=utf8’//设置字符集为utf8;
})
response.write(‘hello Node.js’)//在页面中显示“hello Node.js”证明调用成功
response.end()//设置返回值
}).listen(port,hostname,() => {
// 参数: 端口 域名 监听回调
console.log(The Server is running at: http://${ hostname }:${ port }
)
})