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

Posted zhangzhouy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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`)

  )

 

以上是关于使用Node.js原生代码实现静态服务器的主要内容,如果未能解决你的问题,请参考以下文章