NodeJS中的自动模块导出

时间:2018-04-16 18:00:32

标签: 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(`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
};

有没有办法用某种包或插件自动导出这些?

0 个答案:

没有答案