NodeJS创建 HTTP 服务器

 nodejs不像  .net  java需要一个单独的IIS或者tomcat;它能直接创建一个http服务器

代码如下:

var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'Content-type' : 'text/html'});
res.write('<h1>我是学习nodejs的菜鸟</h1>');
res.end('<p>Hello World</p>');
}).listen(3000);

另存为server.js  中文需要另存为utf-8或者GBK编码才支持中文

cmd 启动服务

chrome 访问

 

如果访问为乱码请设置浏览器的编码为utf-8

原文地址:https://www.cnblogs.com/Martincheng/p/4641642.html