Mongodb atlas + node.js 在本地工作但在推送到 Heroku 时停止 [英] Mongodb atlas + node.js working locally but stop when pushed to Heroku
问题描述
我在我的 node.js 网站中创建了一个 post 路由,允许用户申请工作.它在本地运行良好并连接到 mongodb atlas,但是当我将应用程序推送到 Heroku 并尝试提交工作申请表时,我的网站超时.我对此很陌生,不知道该怎么做.谢谢
I created a post route in my node.js website that allows users to apply for a job. It was working great locally and connecting to mongodb atlas but when I pushed the app to Heroku and try to submit the form for the job application my website times out. I am fairly new at this and do not know what to do. Thanks
这是我的代码
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var Applicant = require('./models/applicants');
var sendAppliedEmail = require('./mail/index.js');
var app = express();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + "/public"));
app.use(express.static(__dirname + '/js'));
var port = process.env.PORT || 3000;
mongoose.connect('mongodb+srv://klaurtar:************@cluster0-nakj7.mongodb.net/test?retryWrites=true', {useNewUrlParser: true});
app.use(bodyParser.urlencoded({extended: true}));
这是我运行 heroku 日志时的终端
here is my terminal when i run heroku logs
推荐答案
我使用 mongodb atlas 将我的 IP 列入白名单并更新了我的代码以将 uri 保存为变量,现在它可以在 Heroku 产品中使用.这是我最终成功的代码.
I whitelisted my IP with mongodb atlas and updated my code to save the uri as a variable and it now works in Heroku production. Here is my code that ended up working.
var uri = "mongodb+srv://klaurtar:************@cluster0-nakj7.mongodb.net/test?
retryWrites=true";
mongoose.connect(uri, {useNewUrlParser: true});
var db = mongoose.connection;
这篇关于Mongodb atlas + node.js 在本地工作但在推送到 Heroku 时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!