使用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}`)

  })

 

posted on 2019-08-15 08:03  山本耀司的清晨  阅读(112)  评论(0编辑  收藏  举报

导航