Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS中的自动模块导出_Node.js_Express - Fatal编程技术网

Node.js NodeJS中的自动模块导出

Node.js NodeJS中的自动模块导出,node.js,express,Node.js,Express,我有一个控制器 import fetch from 'node-fetch' import faker from 'faker' import bodyParser from 'body-parser' import request from 'request-promise' import _ from 'lodash' async function getAccounts(req, res) { try { const response = await fetch(

我有一个控制器

import fetch from 'node-fetch'
import faker from 'faker'
import bodyParser from 'body-parser'
import request from 'request-promise'
import _ from 'lodash'

async function getAccounts(req, res) {
    try {
        const response = await fetch(`http://www.api.com/accounts`);
        const json = await response.json();
        console.log(`${json}`);
        res.send(json)
    } catch (error) {
      console.log(error);
    }
};

async function getAccount(req, res) {
    try {
        const response = await fetch(`http://www.api.com/account/${req.params.id}`);
        const json = await response.json();
        console.log(`${json}`);
        res.send(json)
    } catch (error) {
      console.log(error);
    }
};

async function store(req, res) {
    try {

        var account = {};
        account.account_id                = Number(req.params.id);
        account.email_address             = faker.internet.email().toLowerCase();
        account.password                  = 'benu123';
        account.account_type              = 'customer';
        account.name_prefix               = faker.name.prefix();
        account.first_name                = faker.name.firstName();
        account.middle_names              = '';
        account.last_name                 = faker.name.lastName();
        account.name_suffix               = faker.name.suffix();
        account.non_person_name           = false;
        account.DBA                       = '';
        account.display_name              = faker.name.firstName() + faker.name.lastName();
        account.address1                  = faker.address.streetAddress();
        account.address2                  = faker.address.secondaryAddress();
        account.address3                  = '';
        account.city                      = faker.address.city();
        account.state                     = faker.address.state();
        account.postal_code               = faker.address.zipCode();
        account.nation_code               = faker.address.countryCode();
        account.phone1                    = faker.phone.phoneNumber();
        account.phone2                    = faker.phone.phoneNumber();
        account.phone3                    = faker.phone.phoneNumber();
        account.time_zone_offset_from_utc = -5;
        account.customer_type             = 4;
        account.longitude                 = Number(faker.address.longitude());
        account.latitude                  = Number(faker.address.latitude());
        account.altitude                  = Number(faker.address.longitude());

        var options = {
            method: 'POST',
            uri: 'http://www.api.com/account',
            body: {
                some: account
            },
            json: true // Automatically stringifies the body to JSON
        };

        request(options)
            .then(function () {
                // console.log(account.getAccounts())
                // res.send(account.getAccounts())

            })
            .catch(function (err) {
                console.log(err)
                res.send(err)
            });

    } catch (error) {
      console.log(error);
    }
};

async function update(req, res) {
    try {
        const response = await fetch(`http://www.api.com/account/${req.params.id}`);
        const json = await response.json();
        console.log(bodyParser.json());
        res.send(bodyParser.json())
    } catch (error) {
      console.log(error);
    }
};

async function destroy(req, res) {
    try {
        const response = await fetch(`http://www.api.com/account/${req.params.id}`);
        const json = await response.json();
        console.log(`${json}`);
        res.send(json)
    } catch (error) {
      console.log(error);
    }
};

module.exports = {
    getAccounts,
    getAccount,
    store,
    update,
    destroy
};
我在NodeJS中有5个函数,最后我必须像这样导出它们

module.exports = {
    getAccounts,
    getAccount,
    store,
    update,
    destroy
};

有没有一种方法可以通过某种包或插件自动导出这些文件

export async function example(){}
不适合您?什么是
example()
意思?您的意思是将
export
关键字放在每个函数上?如果你是这个意思,我试过了,但它不起作用。你可以试试module.exports.functionName=function(){//yourcode}