---
title: "node.js第二课（入门）"
author: "mywebcode"
date: "2014-04-13T19:43:00Z"
tags: []
slug: "node-js"
draft: false
lastmod: "2014-04-19T16:32:40Z"
id: 2049257
url: "https://mywebcode.iteye.com/blog/2049257"
---

﻿﻿

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('&lt;h1&gt;hello，world!&lt;/h1&gt;');

res.end('&lt;p&gt;node.jsT&lt;/p&gt;');

}).listen(3000);

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

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

4.调试代码：

npm install -supervisor -g

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

使用supervisor app.js启动