【问题标题】:How to add Mlab url correctly to Heroku?如何将 Mlab url 正确添加到 Heroku?
【发布时间】:2018-09-21 05:09:57
【问题描述】:

我正在尝试使用 Mlab 数据库将一个小型节点应用程序部署到 Heroku - 但我不知道出了什么问题...我在 Heroku 日志中遇到错误,我猜它们与连接有关。 ..because没有它 - 它正在工作。

这是我的步骤和代码:

  1. 我已经在 Heroku 中创建了应用程序 - 按照说明操作
  2. 在 heroku 资源中,我添加了 Mlab 插件并在配置变量中 - 我已将 MONGODB_URI 更改为我的数据库。
  3. 我已将 MONGODB_URI 设置为连接 url...

这里是服务器:

    var express    = require('express');
    var cors = require('cors')

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

    app.use(morgan('dev'));

    var port     = process.env.PORT || 3007; 
    var mongoose   = require('mongoose');

    //MONGOLAB_URI='mongodb://foo:fff@ds163612.mlab.com:63615/cars';
    mongoose.connect(MONGOLAB_URI, function (error) {
        if (error) console.error(error);
        else console.log('mongo connected');
    });


    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));

    db.once('open', function() {
    console.log("DB connection alive");
    });

    // var User     = require('./model/user');
    var Message     = require('./model/message');
    var Car    = require('./model/car');

    var router = express.Router();

    app.get('/',cors(),function(req, res) {

        Car.find(function(err, cars) {
            if (err)
                res.send(err);

            res.json(cars);
        });
    });
        app.get('/messages',cors(),function(req, res) {
            Message.find(function(err, messages) {
                if (err)
                    res.send(err);

                res.json(messages);
            });
        });

        app.get('/cars',cors(),function(req, res) {
            Car.find(function(err, cars) {
                if (err)
                    res.send(err);

                res.json(cars);
            });
        });




    app.listen(port);
    console.log('Magic happens on port ' + port);

这里是日志:

2018-02-16T08:28:52.841304+00:00 app[api]: Release v31 created by user  foo@gmail.com
            2018-02-16T08:28:52.841304+00:00 app[api]: Deploy 5f779472 by user  foo@gmail.com
            2018-02-16T08:28:42.000000+00:00 app[api]: Build succeeded
            2018-02-16T08:28:53.326343+00:00 heroku[web.1]: Restarting
            2018-02-16T08:28:53.327265+00:00 heroku[web.1]: State changed from up to starting
            2018-02-16T08:28:54.189659+00:00 heroku[web.1]: Stopping all processes with SIGTERM
            2018-02-16T08:28:54.296051+00:00 heroku[web.1]: Process exited with status 143
            2018-02-16T08:28:55.360118+00:00 heroku[web.1]: Starting process with command `npm start`
            2018-02-16T08:28:57.304833+00:00 app[web.1]:
            2018-02-16T08:28:57.304857+00:00 app[web.1]: > react-deploy-test@1.0.0 start /app
            2018-02-16T08:28:57.304861+00:00 app[web.1]: > node server.js
            2018-02-16T08:28:57.304862+00:00 app[web.1]:
            2018-02-16T08:28:57.847783+00:00 app[web.1]: Magic happens on port 27591
            2018-02-16T08:28:57.941605+00:00 app[web.1]: DB connection alive
            2018-02-16T08:28:57.941955+00:00 app[web.1]: mongo connected
            2018-02-16T08:28:58.502415+00:00 heroku[web.1]: State changed from starting to up
            2018-02-16T08:31:01.000000+00:00 app[api]: Build started by user  foo@gmail.com
            2018-02-16T08:31:11.871346+00:00 app[api]: Deploy 30ad9492 by user  foo@gmail.com
            2018-02-16T08:31:11.871346+00:00 app[api]: Release v32 created by user  foo@gmail.com
            2018-02-16T08:31:01.000000+00:00 app[api]: Build succeeded
            2018-02-16T08:31:13.173487+00:00 heroku[web.1]: Restarting
            2018-02-16T08:31:13.174022+00:00 heroku[web.1]: State changed from up to starting
            2018-02-16T08:31:14.080217+00:00 heroku[web.1]: Stopping all processes with SIGTERM
            2018-02-16T08:31:14.203672+00:00 heroku[web.1]: Process exited with status 143
            2018-02-16T08:31:15.886400+00:00 heroku[web.1]: Starting process with command `npm start`
            2018-02-16T08:31:18.456384+00:00 app[web.1]:
            2018-02-16T08:31:18.456429+00:00 app[web.1]: > react-deploy-test@1.0.0 start /app
            2018-02-16T08:31:18.456431+00:00 app[web.1]: > node server.js
            2018-02-16T08:31:18.456433+00:00 app[web.1]:
            2018-02-16T08:31:19.161125+00:00 app[web.1]: Magic happens on port 45873
            2018-02-16T08:31:19.164986+00:00 app[web.1]: TypeError: Parameter "url" must be a string, not undefined
            2018-02-16T08:31:19.164990+00:00 app[web.1]:     at Url.parse (url.js:103:11)
            2018-02-16T08:31:19.164992+00:00 app[web.1]:     at Object.urlParse [as parse] (url.js:97:13)
            2018-02-16T08:31:19.164994+00:00 app[web.1]:     at module.exports (/app/node_modules/mongodb/lib/url_parser.js:13:23)
            2018-02-16T08:31:19.164997+00:00 app[web.1]:     at new Promise (<anonymous>)
            2018-02-16T08:31:19.164995+00:00 app[web.1]:     at Promise (/app/node_modules/mongoose/lib/connection.js:332:5)
            2018-02-16T08:31:19.164999+00:00 app[web.1]:     at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:331:19)
            2018-02-16T08:31:19.165001+00:00 app[web.1]:     at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:206:15)
            2018-02-16T08:31:19.165002+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:14:10)
            2018-02-16T08:31:19.165004+00:00 app[web.1]:     at Module._compile (module.js:643:30)
            2018-02-16T08:31:19.165006+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:654:10)
            2018-02-16T08:31:19.165007+00:00 app[web.1]:     at Module.load (module.js:556:32)
            2018-02-16T08:31:19.165009+00:00 app[web.1]:     at tryModuleLoad (module.js:499:12)
            2018-02-16T08:31:19.165010+00:00 app[web.1]:     at Function.Module._load (module.js:491:3)
            2018-02-16T08:31:19.165012+00:00 app[web.1]:     at Function.Module.runMain (module.js:684:10)
            2018-02-16T08:31:19.165014+00:00 app[web.1]:     at startup (bootstrap_node.js:187:16)
            2018-02-16T08:31:19.165015+00:00 app[web.1]:     at bootstrap_node.js:608:3
            2018-02-16T08:31:19.167672+00:00 app[web.1]: (node:20) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Parameter "url" must be a string, not undefined
            2018-02-16T08:31:19.167809+00:00 app[web.1]: (node:20) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
            2018-02-16T08:31:19.566292+00:00 heroku[web.1]: State changed from starting to up
            2018-02-16T08:32:28.000000+00:00 app[api]: Build started by user foo@gmail.com
            2018-02-16T08:32:40.526330+00:00 app[api]: Deploy 3fe92549 by user  foo@gmail.com
            2018-02-16T08:32:40.526330+00:00 app[api]: Release v33 created by user  foo@gmail.com
            2018-02-16T08:32:41.917774+00:00 heroku[web.1]: Restarting
            2018-02-16T08:32:41.919583+00:00 heroku[web.1]: State changed from up to starting
            2018-02-16T08:32:28.000000+00:00 app[api]: Build succeeded
            2018-02-16T08:32:42.673705+00:00 heroku[web.1]: Stopping all processes with SIGTERM
            2018-02-16T08:32:42.777505+00:00 heroku[web.1]: Process exited with status 143
            2018-02-16T08:32:45.105809+00:00 heroku[web.1]: Starting process with command `npm start`
            2018-02-16T08:32:47.705781+00:00 app[web.1]:
            2018-02-16T08:32:47.705800+00:00 app[web.1]: > react-deploy-test@1.0.0 start /app
            2018-02-16T08:32:47.705802+00:00 app[web.1]: > node server.js
            2018-02-16T08:32:47.705804+00:00 app[web.1]:
            2018-02-16T08:32:49.079895+00:00 app[web.1]: Magic happens on port 48220
            2018-02-16T08:32:49.083923+00:00 app[web.1]: Error: Username contains an illegal unescaped character
            2018-02-16T08:32:49.083926+00:00 app[web.1]:     at parseConnectionString (/app/node_modules/mongodb/lib/url_parser.js:274:11)
            2018-02-16T08:32:49.083929+00:00 app[web.1]:     at parseHandler (/app/node_modules/mongodb/lib/url_parser.js:113:14)
            2018-02-16T08:32:49.083932+00:00 app[web.1]:     at Promise (/app/node_modules/mongoose/lib/connection.js:332:5)
            2018-02-16T08:32:49.083930+00:00 app[web.1]:     at module.exports (/app/node_modules/mongodb/lib/url_parser.js:19:12)
            2018-02-16T08:32:49.083934+00:00 app[web.1]:     at new Promise (<anonymous>)
            2018-02-16T08:32:49.083936+00:00 app[web.1]:     at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:331:19)
            2018-02-16T08:32:49.083938+00:00 app[web.1]:     at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:206:15)
            2018-02-16T08:32:49.083940+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:14:10)
            2018-02-16T08:32:49.083942+00:00 app[web.1]:     at Module._compile (module.js:643:30)
            2018-02-16T08:32:49.083944+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:654:10)
            2018-02-16T08:32:49.083945+00:00 app[web.1]:     at Module.load (module.js:556:32)
            2018-02-16T08:32:49.083947+00:00 app[web.1]:     at tryModuleLoad (module.js:499:12)
            2018-02-16T08:32:49.083949+00:00 app[web.1]:     at Function.Module._load (module.js:491:3)
            2018-02-16T08:32:49.083951+00:00 app[web.1]:     at Function.Module.runMain (module.js:684:10)
            2018-02-16T08:32:49.083952+00:00 app[web.1]:     at startup (bootstrap_node.js:187:16)
            2018-02-16T08:32:49.083954+00:00 app[web.1]:     at bootstrap_node.js:608:3
            2018-02-16T08:32:49.086111+00:00 app[web.1]: (node:20) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Username contains an illegal unescaped character
            2018-02-16T08:32:49.086216+00:00 app[web.1]: (node:20) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
            2018-02-16T08:32:49.681542+00:00 heroku[web.1]: State changed from starting to up
            2018-02-16T08:34:01.000000+00:00 app[api]: Build started by user foo@gmail.com
            2018-02-16T08:34:12.813906+00:00 heroku[web.1]: Restarting
            2018-02-16T08:34:12.814458+00:00 heroku[web.1]: State changed from up to starting
            2018-02-16T08:34:12.577577+00:00 app[api]: Release v34 created by user  foo@gmail.com
            2018-02-16T08:34:12.577577+00:00 app[api]: Deploy d3e91a9d by user  foo@gmail.com
            2018-02-16T08:34:01.000000+00:00 app[api]: Build succeeded
            2018-02-16T08:34:13.936150+00:00 heroku[web.1]: Stopping all processes with SIGTERM
            2018-02-16T08:34:14.047866+00:00 heroku[web.1]: Process exited with status 143
            2018-02-16T08:34:16.448867+00:00 heroku[web.1]: Starting process with command `npm start`
            2018-02-16T08:34:19.712447+00:00 app[web.1]:
            2018-02-16T08:34:19.712477+00:00 app[web.1]: > react-deploy-test@1.0.0 start /app
            2018-02-16T08:34:19.712479+00:00 app[web.1]: > node server.js
            2018-02-16T08:34:19.712481+00:00 app[web.1]:
            2018-02-16T08:34:20.739041+00:00 app[web.1]: Magic happens on port 13708
            2018-02-16T08:34:20.859749+00:00 app[web.1]: DB connection alive
            2018-02-16T08:34:20.860347+00:00 app[web.1]: mongo connected
            2018-02-16T08:34:21.269185+00:00 heroku[web.1]: State changed from starting to up
            2018-02-16T08:34:12.813906+00:00 heroku[web.1]: Restarting
            2018-02-16T08:34:12.814458+00:00 heroku[web.1]: State changed from up to starting
            2018-02-16T08:34:12.577577+00:00 app[api]: Release v34 created by user  foo@gmail.com
            2018-02-16T08:34:12.577577+00:00 app[api]: Deploy d3e91a9d by user  foo@gmail.com
            2018-02-16T08:34:01.000000+00:00 app[api]: Build succeeded
            2018-02-16T08:34:13.936150+00:00 heroku[web.1]: Stopping all processes with SIGTERM
            2018-02-16T08:34:14.047866+00:00 heroku[web.1]: Process exited with status 143
            2018-02-16T08:34:16.448867+00:00 heroku[web.1]: Starting process with command `npm start`
            2018-02-16T08:34:19.712447+00:00 app[web.1]:
            2018-02-16T08:34:19.712477+00:00 app[web.1]: > react-deploy-test@1.0.0 start /app
            2018-02-16T08:34:19.712479+00:00 app[web.1]: > node server.js
            2018-02-16T08:34:19.712481+00:00 app[web.1]:
            2018-02-16T08:34:20.739041+00:00 app[web.1]: Magic happens on port 13708
            2018-02-16T08:34:20.859749+00:00 app[web.1]: DB connection alive
            2018-02-16T08:34:20.860347+00:00 app[web.1]: mongo connected
            2018-02-16T08:34:21.269185+00:00 heroku[web.1]: State changed from starting to up

【问题讨论】:

  • 使用process.env.MONGOLAB_URI 而不是MONGOLAB_URI
  • @JitendraKumar 谢谢 - 我也试过了......
  • 您是否在 Heroku 仪表板的应用程序设置下配置了配置变量?
  • 是的,一旦我添加了 mlab 插件 - 它会自动使用一些 mlab 地址创建变量...- 所以我编辑了地址并将其替换为我自己的...

标签: node.js heroku


【解决方案1】:

关于:Error: Username contains an illegal unescaped character 只是回答这个问题,以防其他人遇到这个问题。前几天我遇到了这个问题,结果我在复制/粘贴时不小心在我的环境变量字符串中加入了换行符。当您正在寻找解决方案时,这绝对是值得尝试的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 2017-05-30
    • 1970-01-01
    • 2015-10-24
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多