简体   繁体   English

nodejs 需要在对象文字中并使用它?

[英]nodejs require inside a object literal and using it?

using require like this below , can it cause any issues ?使用下面这样的要求,会导致任何问题吗?

module.exports = {
    _                   : require('lodash'),
    debug               : require('debug'),
    moment              : require('moment'),
    jwt                 : require('jsonwebtoken'),
    bcrypt              : require('bcrypt'),
    SwaggerExpress      : require('swagger-express-mw'),
    express             : require('express'),
    Sequelize           : require('sequelize'),
    SequelizeImport     : require('sequelize-auto-import'),
    twilio              : require('twilio'),
    DataTransform       : require("node-json-transform").DataTransform,
    joi                 : require('joi'),
    joiDateExtension    : require('joi-date-extensions'),
    joiPhoneExtension   : require('joi-phone-number-extensions'),
    requireAll          : require('require-all'),
    leftPad             : require('left-pad'),
    cors                : require('cors'),
    sendgrid            : require('@sendgrid/mail'),
    randomstring        : require('randomstring'),
    withPagination      : require('sequelize-cursor-pagination'),
    razorpay            : require('razorpay'),
    axios               : require('axios'),
    SlackNode           : require('slack-node'),
    emojiStrip          : require('emoji-strip'),
    firebase            : admin,
    apn                 : apn,
    apnProvider         : apnProvider
    // aws                 : AWS,
    // multer              : require('multer')
};

m assigning it to express instance and using it. m 分配它来表达实例并使用它。

const moment        = App.packages.moment;
const firebase      = App.packages.firebase;
const _             = App.packages._;
const emojiStrip    = App.packages.emojiSt

This is formally uncorrect.这在形式上是不正确的。

See Express docs:请参阅 Express 文档:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello world')
})

app.listen(3000)

You can of course experiment different things but you need to know the risk.你当然可以尝试不同的东西,但你需要知道风险。 Always try to stick to common standards.始终尝试坚持共同的标准。

There is no problem in multiple requiring of packages in different files as they are just pulled from the node_modules folder, you are not creating multiple instances.在不同文件中多次要求包没有问题,因为它们只是从node_modules文件夹中提取的,您不会创建多个实例。

Any how module is loaded once.任何模块加载一次的方式。 so I don't think it will cause an issue.所以我认为这不会引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM