如何在主文件Index.js

时间:2018-09-22 09:58:14

标签: javascript node.js node-modules

我需要从主index.js文件中调用模块

这是我的模块

const request = require('./rq.js');
const callback = require('./callback.js')

const url = `https://localhost.3000/${id}`;


request(url, callback)
.then(res => {
    console.log(res);
})
.catch(err => {
    console.log(err);
})


module.exports = page; //Tell me how to export all code from module

这是我的index.js文件

const Methods = {
  page: require('./page.js'),
}


module.exports = //What i need to code here?

我给模块打电话的文件:

const main = require('./index.js');

main.page({id: 'id'})

.then(console.log);

那么我该如何更改以调用page.js文件?

1 个答案:

答案 0 :(得分:0)

对page.js进行以下更改,因为您希望在主文件中返回一个承诺。

const request = require('./rq.js');
const callback = require('./callback.js')

function page({id}) {
    const url = `https://localhost.3000/${id}`;
    return request(url, callback)
}

module.exports = {page: page} //Tell me how to export all code from module

对Mehods.js进行以下更改

const Methods = {
  page: require('./page.js').page,
}


module.exports = Methods;

检查是否可行。