Nodejs服务器端脚本

首先是安装,安装很简单,下载一个msi文件后一路下一步,没有难度,

测试的时候,如果你发现你的环境变量里面没有自动添加进去,也可以进行手动添加环境变量

之后在命令窗口输入:

得到nodejs的版本就说明安装成功了。

然后是第一个hello world测试文件,新建一个文件server.js,下面是代码,注意不需要加上<script>标签,直接写下面的代码

var http = require('http');

http.createServer(function (request, response) {

	// 发送 HTTP 头部 
	// HTTP 状态值: 200 : OK
	// 内容类型: text/plain
	response.writeHead(200, {'Content-Type': 'text/plain'});

	// 发送响应数据 "Hello World"
	response.end('Hello World
');
}).listen(8888);

// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

  然后cmd打开控制窗口,找到文件的根路径然后进行编译 ,例如这是我的文件的跟路径,所以就在控制器里面输入这个

C:Program Files
odejs>node server.js

会自动提示出:

Server running at http://127.0.0.1:8888/

然后再浏览器输入

http://127.0.0.1:8888/

  就能够打赢出   "Hello world" 

原文地址:https://www.cnblogs.com/sunxun/p/4844546.html