加载模块
var express = require(‘express‘); var fs = require(‘fs‘); var path = require(‘path‘); var http = require(‘http‘);
练习1:本地服务器
var app = express(); var config = require(‘./config/config.js‘); app = config(app); app.listen(); //启动监听端口 console.log("端口已启动");
练习2:读写文本
//读取文本 fs.readFile(path.join(__dirname,‘/data/test.json‘),{encoding:‘utf-8‘},function(err,data){ if(err) throw err; console.log(data); }); //写入文本 fs.writeFile(path.join(__dirname,‘/data/test.json‘),"Hello World",function(err){ if(err) throw err; console.log("OK"); });
练习3:创建简单的http服务器
http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/plain‘}); response.end(‘Hello World!‘); }).listen(‘6634‘); console.log("server runing");
练习4:常规断点调试
http.createServer(function(request,response){ response.writeHead(200,{‘Content-Type‘:‘text/plain‘}); debugger response.end(‘Hello World!‘); }).listen(‘6634‘);
练习5:超级调试
先下载安装插件
sudo npm install -g node-inspector
启动插件
node-inspector
新建控制台
command+n
启动练习4的项目
node --debug app.js
google浏览器:http://localhost:8080/debug?port=5858
原文:http://www.cnblogs.com/CyLee/p/5324984.html