Node.js 小记

下面来简单说下有关Node.js的基础;

安装node.js就不赘述了,require函数用于获取、加载模块,exports用于模块的导出;

创建服务器:


var http=require("http");//使用require指令来导入http模块
http.createServer(function(request,response){//创建http server ,传入回调函数
     response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});//设置请求头
     if(request.url!=='/favicon.ico'){//清除第二次访问
         console.log("on!");
         response.write("hello world!");
         response.end("");//结束响应,如果不调用该函数,将永远处于等待状态;
     }
}).listen(8000);//服务器监听8000端口号
console.log("Server running at 127.0.0.1:8000");

这里的有个判断if(request.url!=='/favicon.ico'),这个是防止第二次访问;如未作判断,演示图如下:

第二次访问的结果演示

不写response.end(""),会导致页面一直处于等待状态

 

函数的调用

require进行引入;

var http=require("http");
var modules=require("./model/funcall.js");//引入外部文件
http.createServer(function(request,response){
	response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});	
	if(request.url!=='/favicon.ico'){
        //这里还有一种以对象的方式写:otherFuns[fun2](response);
		modules.fun1(response);
		modules.fun2(response);
		response.end("");
	}
}).listen(8000);
console.log("Server running at 127.0.0.1:8000");

funcall.js  注意这里要用module.exports={}进行导出;

module.exports={
	fun1:function(res){
         res.write("i am fun1"+"</br>");
	},	
	fun2:function(res){
         res.write("i am fun2"+"</br>");
	}
}

 

模块的调用

fun1.js

function Fun1(){
	this.name='lily';
	this.age=19;
}
module.exports=Fun1;

fun2.js,引入fun1.js,改变this指向,也可以用apply方法;

var Fun1=require("./fun1.js");
function Fun2(){
	this.name='tom';
	this.age=18;
	Fun1.call(this);
	this.msg=function(res){
		res.write(this.name+",今年"+this.age);
	}
}
module.exports=Fun2;

 node_demo.js文件部分代码:

var Fun2=require("./model/Fun2.js");//前面引入Fun模块

fun2=new Fun2();
fun2.msg(response);
response.end('');

执行node_demo.js文件,输出结果:

文件 

异步方法最后一个参数是回调函数,回调函数的第一个参数包含错误信息;

下面就列举歌例子吧,包括文件的写入、读取、打开、关闭、删除;

创建user.js模块

var optfile=require("./optfile.js");
module.exports={
	writefile:function(req,res){
		function recall(data){
			res.write(data);
			console.log(data);
			res.end('');
		}
		optfile.writefile('./model/writeFile/one.txt','hello world!',recall);
	},
	openfile:function(req,res){
		function recall(data){
			res.write(data);
			console.log(data);
			res.end('');
		}
		optfile.openfile('./model/writeFile/one.txt',recall);
	},
	unlinkfile:function(req,res){
		function recall(data){
			res.write(data);
			console.log(data);
			res.end('');
		}
		optfile.unlinkfile('./model/writeFile/one.txt',recall);
	}
}

optfile.js模块

var fs=require('fs');
module.exports={
	//异步写入文件
	writefile:function(path,data,recall){
		fs.writeFile(path,data,function(err,data){//data为要写入文件的数据
			if(err){
				throw err;
			}
			console.log('写入文件成功!');
			fs.readFile(path,function(err,data){
				if(err){
					return console.log(err);
				}
				recall("这是刚刚写入的内容:"+data.toString());
			});
		});
	},
	//异步打开文件
	openfile:function(path,recall){
		fs.open(path,function(err,fd){
			if(err){
				return console.log(err);
			}
			console.log('文件已打开!');
			fs.close(fd,function(err){
				if(err){
					return console.log(err);
				}
				recall("文件已关闭");
			})
		});
	},
	//异步删除文件
	unlinkfile:function(path,recall){
		fs.unlink(path,function(err){
			if(err){
				return console.log(err);
			}
			recall("文件已删除!");
		})
	}
}

创建并执行node_demo.js

这里的node_demo模块里,通过筛选路由参数名,以对象的形式进行调用函数;

var http=require("http");
var url=require("url");
var User=require("./model/user.js");
http.createServer(function(request,response){
	response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});	
	if(request.url!=='/favicon.ico'){
	var pathname=url.parse(request.url).pathname;
		pathname=pathname.replace(/\//,'');
		if(pathname){
            User[pathname](request,response);
		}else{
			response.write("访问成功!");
			response.end('');
		}
	}
}).listen(8000);
console.log("访问成功!");

页面输入localhost:8000,输出结果:

 

页面输入localhost:8000/wiritefile,写入one.txt,输出结果:

 

页面输入localhost:8000/unlinkfile,删除one.txt,输出结果:

 

fs.readFile(读取文件)时:需传需要读取的文件路径;

fs.writeFile(写入文件)时:需传文件名、需要写入的数据(数据形式有String类型和Buffer 对象),若不传写入数据则会报错;

fs.close(关闭文件)时:fd是fs.open()方法返回的文件描述符;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值