ITeye博客

node.js第二课(入门)

mywebcode · 2014-04-13 19:43 · Markdown



1.Hello World

打一个一个文本编辑器,在其中输入

console.log('Hello World');

并保存为helloworld.js。打开dos窗口进入该文件的目录运行

node helloworld.js 执行则可以看到输出的helloworld

2.Node.js命令行工具

node -v 版本

node -e eval scipt eval(“console.log('呵呵')”);

例:node -e "console.log('hello world')";直接执行

node 直接进入编译模式

console.log("111")

第一行是输出、第二行是返回值

3.建立HTTP服务器

创建一个app.js

var http=require('http')

http.createServer(function(req,res){

res.writeHead(200,{'Content-Type':'text/html'});

res.write('<h1>hello,world!</h1>');

res.end('<p>node.jsT</p>');

}).listen(3000);

console.log('HTTP server is listening at port 3000.');

接下来node app.js 打开浏览器访问 http://localhost:3000即可。这样就部署了一个web。比起tomcat、resin更加方便

4.调试代码:

npm install -supervisor -g

安装supervisor 来控制调试代码。 不需要每次停止重启node.js的服务

使用supervisor app.js启动

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。