教程
node.js中的dns.getServers方法使用说明 node.js中的events.EventEmitter.listenerCount方法使用说明 node.js中的path.resolve方法使用说明 node.js中的path.sep方法使用说明 node.js中的path.normalize方法使用说明 node.js中的path.join方法使用说明 node.js中的path.isAbsolute方法使用说明 node.js中的path.extname方法使用说明 node.js中的path.dirname方法使用说明 node.js中的path.delimiter方法使用说明 node.js中的path.basename方法使用说明 node.js中的console.timeEnd方法使用说明 node.js中的console.time方法使用说明 node.js中的console.trace方法使用说明 node.js中的console.info方法使用说明 node.js中的console.warn方法使用说明 node.js中的console.log方法使用说明 node.js中的console.error方法使用说明 node.js中的console.dir方法使用说明 node.js中的console.assert方法使用说明 node.js中的url.format方法使用说明 node.js中的url.parse方法使用说明 node.js中的url.resolve方法使用说明 node.js中的querystring.parse方法使用说明 node.js中的querystring.stringify方法使用说明 node.js中的querystring.escape方法使用说明 node.js中的querystring.unescape方法使用说明 node.js中的events.emitter.removeAllListeners方法使用说明 node.js中的events.emitter.listeners方法使用说明 node.js中的events.emitter.removeListener方法使用说明 node.js中的emitter.emit方法使用说明 node.js中的events.emitter.once方法使用说明 node.js中的emitter.on方法使用说明 node.js中的http.request.end方法使用说明 node.js中的buffer.write方法使用说明 node.js中的buffer.slice方法使用说明 node.js中的buffer.Buffer.byteLength方法使用说明 node.js中的buffer.Buffer.isBuffer方法使用说明 node.js中的buffer.Buffer.isEncoding方法使用说明 node.js中的buffer.toString方法使用说明 node.js中的buffer.toJSON方法使用说明 node.js中的buffer.length方法使用说明 node.js中的buffer.fill方法使用说明 node.js中的buffer.copy方法使用说明 node.js中的http.request方法使用说明 node.js中的http.response.removeHeader方法使用说明 node.js中的http.response.setHeader方法使用说明 node.js中的http.response.end方法使用说明 node.js中的http.response.write方法使用说明 node.js中的http.response.getHeader方法使用说明 node.js中的http.response.writeHead方法使用说明 node.js中的http.response.addTrailers方法使用说明 node.js中的fs.writeFile方法使用说明 node.js中的fs.writeFileSync方法使用说明 node.js中的fs.writeFile方法使用说明 node.js中的http.createServer方法使用说明 node.js中的http.get方法使用说明 node.js中的http.createClient方法使用说明 node.js中的fs.write方法使用说明 node.js中的fs.writeSync方法使用说明 node.js中的fs.fsyncSync方法使用说明 node.js中的fs.ftruncate方法使用说明 node.js中的fs.fsync方法使用说明 node.js中的fs.unlink方法使用说明 node.js中的fs.unlinkSync方法使用说明 node.js中的fs.truncate方法使用说明 node.js中的fs.truncateSync方法使用说明 node.js中的fs.utimes方法使用说明 node.js中的fs.utimesSync方法使用说明 node.js中的fs.symlink方法使用说明 node.js中的fs.symlinkSync方法使用说明 node.js中的fs.link方法使用说明 node.js中的fs.linkSync方法使用说明 node.js中的fs.readFile方法使用说明 node.js中的fs.readFileSync方法使用说明 node.js中的fs.fstat方法使用说明 node.js中的fs.fstatSync方法使用说明 node.js中的require使用详解 node.js中的fs.lstat方法使用说明 node.js中的fs.lstatSync方法使用说明 node.js中的fs.rename方法使用说明 node.js中的fs.renameSync方法使用说明 node.js中的fs.realpath方法使用说明 node.js中的fs.realpathSync方法使用说明 node.js中的fs.stat方法使用说明 node.js中的fs.statSync方法使用说明 node.js中的fs.lchmod方法使用说明 node.js中的fs.lchmodSync方法使用说明 node.js中的fs.fchmod方法使用说明 node.js中的fs.fchmodSync方法使用说明 node.js中的fs.lchown方法使用说明 node.js中的fs.lchownSync方法使用说明 node.js中的fs.chown方法使用说明 node.js中的fs.chownSync方法使用说明 node.js中的fs.fchown方法使用说明 node.js中的fs.fchownSync方法使用说明 node.js中的fs.rmdir方法使用说明 node.js中的fs.rmdirSync方法使用说明 node.js中的fs.mkdir方法使用说明 node.js中的fs.mkdirSync方法使用说明 node.js中的fs.read方法使用说明 node.js中的fs.readSync方法使用说明 node.js中的fs.readlink方法使用说明 node.js中的fs.readlinkSync方法使用说明 node.js中的fs.readdir方法使用说明 node.js中的fs.readdirSync方法使用说明 node.js中的fs.close方法使用说明 node.js中的fs.closeSync方法使用说明 node.js中的fs.open方法使用说明 node.js中的fs.openSync方法使用说明 node.js中的fs.exists方法使用说明 node.js中的fs.existsSync方法使用说明 node.js中的fs.futimes方法使用说明 node.js中的fs.futimesSync方法使用说明 node.js中的fs.createWriteStream方法使用说明 node.js中的fs.createReadStream方法使用说明 node.js中的fs.appendFileSync方法使用说明 node.js中的fs.appendFile方法使用说明 node.js中的fs.chmod方法使用说明 node.js中的fs.chmodSync方法使用说明

发布于 2016-08-25 06:06:54 | 161 次阅读 | 评论: 0 | 来源: 网友投递

本文通过几个具体实例来分析讲解了node.js中require的使用方法,非常的详尽,这里推荐给大家

代码注释里已经描述的非常的清晰,这里就不多废话了,直接奉上代码:



/*在node中,可以使用require()函数来加载模块.

 * require函数使用一个参数,参数值可以带有完整路径的模块的文件名,也可以为模块名.当使用node中提供的模块时,在require函数中只需要指定模块名即可.

 * */

//建立一个页面2.js;代码如下

var name="思思博士";

exports.name=name;

//建立一个页面1.js;代码如下

var two=require("./2.js");

console.log(two.name);

//输出结果:思思博士

/*
 * 在node中所有的脚本文件都是一个模块文件,因此1.js也是一个模块文件,又由于该文件是在命令行窗口中通过node命令被直接运行的,因此在node中该模块文件被定义为应用程序的主模块
 * 可以用如下的方法检测出当前的模块是否是主模块
 * */
if(module===require.main){
    console.log("当前模块时主模块");
}
//输出结果:当前模块时主模块

//2.js代码
var name="思思博士";
console.log(name);
exports.name=name;

//1.js代码:
var two=require("./2.js");
var two=require("./2.js");
//虽然引用了2次,但是只是执行了1次console.log(name)的输出.

/*require.resolve(str)
 * 在node中,可以使用这个函数来查询某个模块文件的带有完整绝对路径的文件名.
 * */
var url=require.resolve("./2");
console.log(url);
//输出结果:E:\node\gys\2.js

/*require.cache
 * 在node中,这个属性代表了所有已被加载模块的缓存区.
 * */

var two=require("./2.js");
var cache=require.cache;
console.log(cache);
/*输出结果:
 * { 'E:\\node\\gys\\1.js':
 { id: '.',
 exports: {},
 parent: null,
 filename: 'E:\\node\\gys\\1.js',
 loaded: false,
 children: [ [Object] ],
 paths:
 [ 'E:\\node\\gys\\node_modules',
 'E:\\node\\node_modules',
 'E:\\node_modules' ] },
 'E:\\node\\gys\\2.js':
 { id: 'E:\\node\\gys\\2.js',
 exports: { name: '思思博士' },
 parent:
 { id: '.',
 exports: {},
 parent: null,
 filename: 'E:\\node\\gys\\1.js',
 loaded: false,
 children: [Object],
 paths: [Object] },
 filename: 'E:\\node\\gys\\2.js',
 loaded: true,
 children: [],
 paths:
 [ 'E:\\node\\gys\\node_modules',
 'E:\\node\\node_modules',
 'E:\\node_modules' ] } }
 * */


//2.js代码
var name="思思博士";
console.log(name);
//1.js代码
//当使用delete关键字删除缓存区中缓存的某个模块对象后,下次加载该模块时将重新运行该模块中的代码.使用代码:

var two=require("./2.js");
var two1=require("./2.js");
console.log("删除前")
delete require.cache[require.resolve("./2.js")];
console.log("删除后");
var two2=require("./2.js");
/*
 * 输出结果:
 * 思思博士
 * 删除前
 * 删除后
 * 思思博士
 * */

小伙伴们是否清楚了node.js中require的使用方法了呢,如有疑问,请留言。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务