Node.js的第一个应用

上一篇介绍了Node.js的环境的搭建。本篇介绍第一个应用测试案例,程序界的启蒙课“Hello World”,这是一种“信仰”,哈哈。

首先,创建test.js脚本。引入http模块,创建服务器并设置好监听。

var http = require('http');
http.createServer(function (request, response){
  response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');

接着运行cmd,启动服务器。

最后打开浏览器输入:http://127.0.0.1:8888 回车,运行结果如下。

 

 经过这个案例测试运行,证明本地Node.js开发环境的搭建是OK的。

猜你喜欢

转载自blog.csdn.net/hppyw/article/details/120906954