简体   繁体   English

如何在 NodeJS 中转换为导入/导出 ES 模块

[英]How to convert to import/export ES Modules in NodeJS

Could someone please explain how to convert to imports in NodeJS using following example?有人可以使用以下示例解释如何在 NodeJS 中转换为导入吗?

const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);

You can do it by the following method.您可以通过以下方法来完成。

 import MongoDBStore from 'connect-mongodb-session';
 import session from 'express-session';

 const MongoStore = MongoDBStore(session);

If you look at the underlying code of connect-mongodb-session which you can find here Github-connect-mogodb-session you will see that this exports something like: module.exports = function(connect) {如果你查看connect-mongodb-session的底层代码,你可以在这里找到Github-connect-mogodb-session,你会看到它导出类似: module.exports = function(connect) {

Which basically says, that it exports (makes available for require import) a function with a parameter named connect.这基本上是说,它导出(可用于 require 导入)一个带有名为 connect 的参数的函数。 In their docs (just above this exports) they say:在他们的文档中(就在这个出口之上)他们说:

/**
 * Returns a constructor with the specified connect middleware's Store
 * class as its prototype
 *
 * ####Example:
 *
 *     connectMongoDBSession(require('express-session'));
 *
 * @param {Function} connect connect-compatible session middleware (e.g. Express 3, express-session)
 * @api public
 */

Showing to the caller what is expected as parameter to this module.向调用者显示该模块的预期参数。 Some more useful examples here: tutorialsteacher这里有一些更有用的例子: tutorialsteacher

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

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