如何正确添加Mlab url到Heroku?

时间:2018-02-16 08:53:30

标签: node.js heroku

我尝试使用Mlab数据库向Heroku部署一个小节点应用程序 - 但我无法弄清楚错误...我在Heroku日志中遇到错误我猜他们与连接有关...没有它就会导致 - 它正在工作。

以下是我的步骤和代码:

  1. 我已经在Heroku中创建了应用程序 - 按照说明进行操作
  2. 在heroku资源中,我添加了Mlab插件和Config Variables - 我已将MONGODB_URI更改为我的数据库。
  3. 我已将MONGODB_URI设置为连接网址
  4. 这是服务器:

        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
    

1 个答案:

答案 0 :(得分:0)

关于:Error: Username contains an illegal unescaped character仅在回答其他人遇到此问题的情况下才回答。前几天我遇到了这个问题,结果发现我在复制/粘贴时不小心在换行符中插入了换行符。寻找修补程序时绝对可以尝试一下。