将heroku nodejs应用程序连接到mlab数据库

时间:2019-12-03 19:21:50

标签: javascript node.js mongodb heroku mlab

我已经在heroku上部署了节点应用程序。当我转到地址https://app.herokuapp.com/api时,我可以看到:

  

“ API的工作原理”,消息:“欢迎使用爱精心打造的RESTHub!”

router.get ('/', function (req, res) {
     res.json ({
         status: 'API Its Working',
         message: 'Welcome to RESTHub crafted with love!',
     });
});

当我尝试转到https: // app.herokuapp.com / api / contacts时,收到错误消息Application error ...。在本地尝试http://localhost:8080/api/contacts可以正常工作时,获取联系人。

router.route('/contacts')
    .get(contactController.index)
    .post(contactController.new);

我有错误:

  

DeprecationWarning:当前的服务器发现和监视引擎是   已过时,将在以后的版本中删除。使用新的   服务器发现和监视引擎,通过选项{   useUnifiedTopology:true}转换为MongoClient构造函数。

     

UnhandledPromiseRejection警告:MongoNetworkError:连接失败   首次连接到服务器[cluster0-shardc.mongodb.net:27017]   [MongoNetworkError:与cluster.mongodb.net:27017的连接4已关闭

api-routes.js

// Initialize express router
let router = require('express').Router();
// Set default API response
router.get('/', function (req, res) {
    res.json({
        status: 'API Its Working',
        message: 'Welcome to RESTHub crafted with love!',
    });
});
// Import contact controller
var contactController = require('./contactController');
// Contact routes
router.route('/contacts')
    .get(contactController.index)
    .post(contactController.new);

router.route('/contacts/:contact_id')
    .get(contactController.view)
    .patch(contactController.update)
    .put(contactController.update)
    .delete(contactController.delete);


// Export API routes
module.exports = router;

0 个答案:

没有答案