我正在使用Nodejs、Expressjs和Mongodb(atlas)的应用程序。Post方法在localhost和Postman上运行良好,但在Heroku上部署应用程序后出现上述错误。请帮助我,提前感谢。
这是我的文件夹结构的图像。folder structure
/数据库/连接. js:
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.HOST,{useNewUrlParser:true, useUnifiedTopology : true})
.then(() => console.log('connection successful'))
.catch((err) => console.log('Error :' +err));
.env文件:
HOST=mongodb+srv://myid:mypassword@cluster0.h6y89rp.mongodb.net/portfolio?retryWrites=true&w=majority
(使用我的ID和密码,而不是原始代码中的“myid:mypassword”)
/源代码/模式/用户. js:
const mongoose = require('mongoose');
const validator = require('validator');
const portfolioSchema = new mongoose.Schema({
name :
{
type : String,
minlength : 3,
maxlength : 50,
required : true,
validate(value)
{
if(validator.isNumeric(value))
{
throw new Error("Name should be alphabatic");
}
}
}, email :
{
type : String,
required : true,
validate(value)
{
if(!validator.isEmail(value))
{
throw new Error('Email not valid');
}
}
},
message : {
type : String
}})
module.exports = new mongoose.model('contact',portfolioSchema);
源代码/应用程序. js:
require('dotenv').config();
const express = require('express');
const app = express();
require('./db/connect');
const user = require('./modals/user');
const port = process.env.PORT || 5000;
const path = require('path');
const public = path.join(__dirname,'../public');
app.use(express.urlencoded({extended : false}));
app.use(express.json());
app.use(express.static(public));
app.set('view engine','hbs');
app.get('/',(req,res)=>{
res.render('index')//views--> hbs file
})
app.post('/',async(req,res)=>
{
const data = new user(req.body);
try{
const result = await data.save();
res.status(201).send(`your data is added , We will contact you soon have nice day :)`);
}
catch(err)
{
res.status(400).send('Something went wrong'+err);
}
})
app.listen(port,() =>{
console.log('server is started at '+port)
});
表单数据位于此文件views/index.hbs中:
<div class="col-lg-6 col-md-6 col-12">
<form action="/" method="post" class="contact-form webform" role="form">
<div class="form-group d-flex flex-column-reverse">
<input type="text" class="form-control" name="name" id="name" placeholder="Your Name">
<label for="cf-name" class="webform-label">Full Name</label>
</div>
<div class="form-group d-flex flex-column-reverse">
<input type="email" class="form-control" name="email" id="email"
placeholder="Your Email">
<label for="cf-email" class="webform-label">Your Email</label>
</div>
<div class="form-group d-flex flex-column-reverse">
<textarea class="form-control" rows="5" name="message" id="message"
placeholder="Your Message"></textarea>
<label for="cf-message" class="webform-label">Message</label>
</div>
<button type="submit" class="form-control" id="submit-button" name="submit">Send</button>
</form>
</div>
package.json文件中的脚本:
"dev" : "nodemon src/app.js -e hbs,js,html",
"start" : "node src/app.js -e hbs,js,html"
两个请求都在 Postman 中工作:
get request in postmanpost request in postman
1条答案
按热度按时间x6492ojm1#
我明白了上面的应用程序有什么问题。
当我们在Heroku上部署应用程序时,我们应该将“mongodb atlas / database url”添加到Heroku中,该文件位于“.env file”env file image中。出于安全考虑,我们不会上传该文件,因此我们需要确保在Heroku中使用“mongodb atlas / database url”进行数据库连接。
以下是在Heroku中添加mongodb url的步骤:
1.登录Heroku(在浏览器上)。
2.click 。
3.点击设置选项卡。Settings tab image
4.单击“显示配置变量”。
5.有两个字段'KEY'和'VALUE'。在键字段添加键,就像在.env文件中一样。在这种情况下,键将是'HOST',在值字段添加mongodb url,就像下面的图像中给出的那样。key value in HEROKU image