nodeJS初体验(解决中文乱码)
2019-07-17 本文已影响0人
安宇辛
一:安装nodeJS
下载地址:http://nodejs.cn/download/

检查版本 $ node -v
二:编写js
helloworld.js

代码如下
// 载入http模块
var http = require("http")
// 创建服务器
http.createServer(function(request, response){
//发送HTTP头部
//HTTP状态:200:OK
//内容类型:text/plain
response.writeHead(200,{'Content-Type': 'text/html;charset=UTF8'});
//发送响应数据
response.end("欢迎来到我的世界!");
}).listen(8000); //服务器在8000端口监听
//终端打印信息
console.log("Server running at http://127.0.0.1:8000/");
注意:text/html;charset=UTF8 是为了解决中文乱码
三:展示

