Node.js初探之hello world

  

  昨天公司内部培训,主讲人王老板对Node.js评价很高,连用几个“变态”来形容,恰好今天周末,有时间来认识下Node.js,对一门新语言最好的认识,是让其输出“hello world”,今天我就利用Node.js输入“hello world”。

  一、安装Node.js

  (1)下载Node.js

  去Node.js官网下载,因为我的电脑是windows 64位win7系统,所以下载是64位安装包。

  

  (2)安装Node.js

  双击下载下来的安装包,选择安装路径,傻瓜似的点击下一步就行了。

  (3)将Node.exe加入环境变量中

  

  二、输出“hello world”

  (1)第一个例子

  建立helloworldDemo.js文件,然后在里面输入:

console.log("hello world");

  

  (2)第二例子

  建立helloworldDemo2.js文件,然后在里面输入:  

var sys = require("sys"),  
    http = require("http");  
http.createServer(function(request, response) {  
    response.writeHead(200, {"Content-Type": "text/html"});  
    response.write("Hello World!");  
    response.end();  
}).listen(8080);  
sys.puts("Server running at http://localhost:8080/");   

  然后我们在命名台中输入命令node helloworldDemo2.js,在浏览器输入http://localhost:8080/

  

  

  三、进一步学习

  至此,简单“hello world”学习已结束,欲进一步学习Node.js,可以查看官网相关文档以及相关书集。

  这里有一本入门级书籍:《Node入门》

  

  

原文地址:https://www.cnblogs.com/danshui/p/3249602.html