1.创建http服务
var http = require('http');
http.createServer(function(req,res){
res.writeHead(200,{"Content-Type":"text/html"});
res.write("<h1>Node.js</h1>");
res.end("<p>Hello World</p>");
}).listen(3000);
console.log("HTTP server is listening at port 3000...");
在cmd模式下执行如下代码:
D:\nodejsdemo>node app.js
HTTP server is listening at port 3000...
在地址栏访问http://localhost:3000/,打印出上面内容
2.supervisor
supervisor模块可以检测源文件修改,不需要每次都执行node app.js才能看见源代码改动
安装:
D:\nodejsdemo>npm install -g supervisor
使用运行app.js
D:\nodejsdemo>supervisor app.js
Running node-supervisor with
program 'app.js'
--watch '.'
--extensions 'node,js'
--exec 'node'
Starting child process with 'node app.js'
Watching directory 'D:\nodejsdemo' for changes.
HTTP server is listening at port 3000...
3.事件使用
var EventEmitter = require("events").EventEmitter;
var event = new EventEmitter();
event.on("some_event",function(){
console.log("some_event occured.");
});
setTimeout(function(){
event.emit("some_event");
},3000)