NodeJS i18next和MongoDB

时间:2018-07-31 20:45:23

标签: node.js mongodb express i18next

当需要MongoDB身份验证时,任何人都可以使用i18next-node-mongodb-backend模块。该模块的文档指出以下内容:

var i18next = require('i18next');
var Backend = require('i18next-node-mongodb-backend');

i18next
  .use(Backend)
  .init({
    backend: options
  });

我使用了hostportdbcollection选项。但是,是否还可以指定用户和密码?

i18next
    .use(mongoBackend)
    .use(i18nextMiddleware.LanguageDetector)
    .init({
        backend: {
            host: 'localhost',
            port: 27017,
            db: 'my_database',
            collection: 'my_collection',
            // authentication option?

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

通过查看i18next-node-mongodb-backend的代码,我可以确认上述注释。解决此问题的唯一方法是使用uri参数=> https://github.com/gian788/i18next-node-mongodb-backend/blob/master/lib/index.js#L52

uri的结构应如下:https://docs.mongodb.com/manual/reference/connection-string/

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]