标签:read cti eve nod Fix css -- event tty
声明了一个模块,用于读取mime.json中的记录 var fs = require(‘fs‘); var events = require(‘events‘); var eventemitter = new events.EventEmitter(); var getmimetype = function (path,eventemitter,suffix) { fs.readFile(path,function (err,data) { if(err){ console.log(err); return false; } var filedata = JSON.parse(data.toString()); eventemitter.emit(‘wenwa‘,filedata[suffix]); //console.log(filedata[suffix]) }) }; module.exports = getmimetype;
也就是在另一个文件中调用写好的模块,并声明广播的接收方 var eventer = require(‘./readfile_events‘); var fs = require(‘fs‘); var events = require(‘events‘); var eventemitter = new events.EventEmitter(); eventer(‘./mime.json‘,eventemitter,‘.css‘); eventemitter.on(‘wenwa‘,function (data) { console.log(data) });
//定义模块
var fs = require(‘fs‘); var gettype = function (suffix,callback) { fs.readFile(‘./mime.json‘,function (err,data) { if(err){ console.log(err); return false; } var filedata = JSON.parse(data.toString()); //console.log(filedata[suffix]); callback(filedata[suffix]) }) }; module.exports = gettype;
//调用模块
var gettype = require(‘./readfilecallback.js‘); var fs = require(‘fs‘); var events = require(‘events‘); var eventemitter = new events.EventEmitter(); gettype(‘.323‘,mycallback); function mycallback(data) { console.log(data) }
node.js接收异步任务结果的两种方法----callback和事件广播
标签:read cti eve nod Fix css -- event tty
原文地址:https://www.cnblogs.com/saintdingspage/p/11737419.html