您的位置:首页 > Web前端 > Node.js

NodeJs初识模块开发

2016-03-08 10:09 549 查看
today, 我们的话题作文是nodejs,我尝试对它的描述是:似曾相似,但它又不一样的存在着。百花齐放的it世界里,以独特身姿绽放的美丽更容易吸引人。也许只是不了解,也许时间会给出答案,nodejs你值得拥有。

在第0篇中聊了一些皮毛的东西之后,潜意识中认为很简单啦,其实,真的也不难。

本篇主要讲模块NodeJS中的modules.

1.http模块

Example:

在NodeJS文件夹下创建server.js文件,内容如下:

var http=require("http");
http.createServer(function(request,response){
response.writeHead(200,{"ContentType":"text/html"});
response.write("<h1>hello ,I am YZR</h1>");
response.end(" This Node JS");
}).listen(8888);

console.log("running");

再次,在nodejs命令行中执行这个脚本:

C:\Users\Administrator>E:
E:\>cd NOdeJs
E:\NodeJS>node server.js
running

最后在浏览器中输入地址:127.0.0.1:8888



2.fs模块(filestream)

Example:

2.1读取文件操作:

在NodeJS文件夹下创建fsRead.js文件,内容如下:

var fs=require("fs");
//同步
console.log("Start Synchronous Read");
var data=fs.readFileSync("my.txt","utf-8");
console.log(data);
console.log("Synchronous read over");
//异步
console.log("Start Asynchronous Read");
fs.readFile("my.txt","utf-8",function(err,data){
if(err){
console.log("error");
}
else{
console.log(data);
}
});
console.log("ASynchronous read end);

//在同目录夹下创建my.txt,里面随便写点东西即可.

再之在nodejs命令行运行此脚本
E:\NodeJS>node fsRead.js
Start Synchronous Read
This is file content.
Synchronous read over
Start Asynchronous Read
ASynchronous read end
This is file content.

2.2 写入文件操作:


创建fsWrite.js文件:


var fs = require("fs") ;
var filePath="E:\\NodeJs\\myFile.txt";
var str="write someting to text file";
//追加形式写入
fs.appendFile(filePath,str,function(err){
if(err){
console.log("fail");
}
else{
console.log("success");
}
});
/*
//替换形式写入
fs.writeFile(filePath,str,function(err){
if(err){
console.log("fail");
}
else{
console.log("success");
}
});
*/


执行脚本之后,查看由filePath指定的文件内容是否写入成功.

补充:fs.writeFileSync(filename, data[, options])和fs.appendFileSync(filename, data[, options])同步方式

在这两个列子中,我们用到了var http=require("http"),var fs=require("fs");其中http,fs是NodeJs的一个核心模块,其内部使用C++实现的,外部用javascript封装。我们可以通过require函数获取这个模块,然后才能调用其中的对象.

模块和文件是一一对应的,换言之,一个Node.js文件就是一个模块,这个文件可能是javascript,json或者编译过的C/C++扩展。
Node.js提供了exports和require两个对象,exports是模块公开的接口,require是用于从外部获取一个模块的接口。

==>使用exports在一个文件内编写代码,这个exports对象将作为一个接口暴露出去。通过require获取exports对象。

3.创建自己的模块

Example:

创建myModule.js(后缀名为.js)文件

//这个文件就是一个模块,这个模块的exports对象可以暴露给外部

var name;

//exports将作为一个接口对象暴露出去
exports.setName=function(_name){
name=_name;
};
exports.sayHello=function(){
console.log("hello "+name);
};

exports.name="LYF";

创建执行脚本app.txt,内容如下:

//使用require来获取模块对象

var YZR=require("./myModule");
console.log("name:"+YZR.name);
YZR.setName("YangZhiRan");
YZR.sayHello();

最后在命令行中执行脚本:

E:\NodeJS>node app.txt
name:LYF
hello YangZhiRan

4.单次加载

require获取的对象实例都是同一个,后者会覆盖前者.
var yzr1=require("myModule");
yzr1.setName("Francis");

var yzr2=require("myModule");
yzr2.setName("LYfeng");

yzr1.sayHello();
//结果是输出LYfeng

5.覆盖exports

稍微留意一下myModule.js文件,我们都是通过exports.对象的形式来封装exports接口。但是,如果我们下次需要封装一个对象返回呢,比如

function People(){

var name="yzr";

var sayHello=function(name){

console.log("hello "+name);

}

}

1.如果采用之前的exports.对象的形式,我们可以这样子写:

exports.People=People;

这样子的话,在使用require("myModule")时,如果我们想要得到People对象,那么我们这样子写:

var people=require("myModule").People;

2.使用覆盖的形式

使用module.exports=People;这样子返回的将会直接是People对象。

var people=require("myModule");

Next:NodeJS中的包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: