[浅学] 1、Node.js尝试_安装&运行第一个helloworld

 

官网:https://nodejs.org/

介绍:Node.js® is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

关键词:后台语言、Chrome's JavaScript runtime、event-driven、non-blocking I/O model

安装方式:windows上进入官网->点击INSTALL按钮直接可以安装

第一个HelloWorld:(一个Web服务器)

1、新建一个example.js的文件,输入下列代码:

1 var http = require('http');
2 http.createServer(function (req, res) {
3   res.writeHead(200, {'Content-Type': 'text/plain'});
4   res.end('Hello World\n');
5 }).listen(1337, '127.0.0.1');
6 console.log('Server running at http://127.0.0.1:1337/');

2、cmd代开命令行,转到上面新建文件的目录下,输入:node example.js (服务器启动后效果如下)

3、上面说了这是个Web服务器,所以在浏览器里可以直接输入http://127.0.0.1:1337/,大家将会看到hello world的界面:

说明:从上面一个简单的例子想必大家已经看出node.js的一个优势,即只用简单的几行代码就构建了一个web服务器,这个如果让jsp来做要用比较多的代码的。

引申:在首页的INSTALL下面还有个API DOCS按钮,大家如果想更细致的玩一下node.js可以从这里开始,同时市面上也有比较多的书~

 

posted @ 2015-06-04 01:57  beautifulzzzz  阅读(1520)  评论(0编辑  收藏  举报