2016-10-26 31 views
1

例如,我有profile.js什么要求()返回实际的文件或功能

var EventEmitter = require("events").EventEmitter; 
var https = require("https"); 
var http = require("http"); 
var util = require("util"); 

    function Profile(username) { 
    // function code here 
    } 

    util.inherits(Profile, EventEmitter); 

    module.exports = Profile; 

在我app.js,我有

var Profile = require("./profile.js"); 


var studentProfile = new Profile("chalkers"); 

/** 
* When the JSON body is fully recieved the 
* the "end" event is triggered and the full body 
* is given to the handler or callback 
**/ 
studentProfile.on("end", console.dir); 

/** 
* If a parsing, network or HTTP error occurs an 
* error object is passed in to the handler or callback 
**/ 
studentProfile.on("error", console.error); 

所以变量profile.js本身还是函数Profile(用户名)?如果profile.js具有不同的功能,例如我在profile.js中具有函数SetProfile(username),那么应该如何导出这两个函数并在app.js中使用它们?

回答

4

require(...)函数返回module.exports值来自“required”模块,并且在Profile函数的情况下。


顺便说一句,我不知道什么是“ 返回文件”或“ 配置文件是profile.js本身”的意思。

+0

如果profile.js具有不同的功能,比如我在profile.js中有函数SetProfile(username),那么应该如何导出这两个函数并在app.js中使用它们? –

+0

'module.exports.x = function x(){}; module.exports.y = function y(){};' – Amit

+0

我可以将它们导入到app.js的一个地方吗?像var Functions = require('./ profile.js'),然后使用Functions.x(),Function.y() –

相关问题