首页 > Web开发 > 详细

NodeJs - 100

时间:2016-03-27 11:04:36      阅读:298      评论:0      收藏:0      [点我收藏+]

加载模块

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

 

 

NodeJs - 100

原文:http://www.cnblogs.com/CyLee/p/5324984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!