使用Node.js原生代码实现静态服务器

const http=require ('http');  //导入模块

const PORT=3000; //设置端口号3000(3000 5000 6000  9000)

const HOSTNAME='localhost'; //或"127.0.0.1"

http.createServer((req,res)=>{ 

  res.writeHead('Content-type:text/html,charset:utf-8');

  res.writeHead( 200, { //设置响应头 状态码!!!!!!!!!!!
  'Content-Type': 'text/html;charset=utf8'  //字符编码 ‘Content-type’:‘text/html;charset:utf-8' ;
  })

  res.write('xxxxxxxx');

  res.end('结束xxx')

      

}).listen(PORT,HOSTNAME,()=>{

console.log(`The Server is running at: http://${ HOSTNAME }:${PORT}`)

  })

原文地址:https://www.cnblogs.com/zhangzhouy/p/11355753.html