所以我有一个Node / Express应用程序,它应该是单独客户端应用程序的后端/路由器。每当我在本地对其进行测试时,该应用程序都可以正常运行。但是,每当我将其上传到Heroku并将其托管在此处时,该应用程序要么无法运行,要么崩溃。最初我以为这是由我使用的称为bcrypt的Node模块引起的,所以我将没有它的应用程序重写到一个全新的目录中,创建了一个新的Heroku页面并上传了新页面,但是该应用程序仍然崩溃。没有日志详细说明它为什么在Heroku上崩溃(或者至少我看不到)。我不确定我的代码是否有问题,但是任何建议都将不胜感激。我的应用程序如下:
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const assert = require('assert');
const dbURI = process.env.MONGODB_URI;
const dbName = 'expresstestdb';
const collName = 'mealPlanner';
const app = express();
MongoClient.connect(dbURI, (err, client)=>{
if(err) throw err;
db = client.db(dbName);
col = db.collection(collName);
});
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', (req, res)=>{
res.send('Express app is running');
});
app.post('/login', (req, res) => { //handles app login
let uName = req.body.username;
let passText = req.body.password;
let searchObj = {};
searchObj['username'] = uName;
col.find(searchObj).toArray((err, result) => {
if(err) return err;
if(result.length == 0){
res.send({"login": false});
}
else{
let hash = result[0].password;
if(hash == passText){
res.send({"login": true, "meals": result[0].meals});
}
else{
res.send({"login": false});
}
}
});
});
app.post('/update', (req, res) => { //handles schedule update
let uName = req.body.username;
let mealObj = req.body.meals;
let searchObj = {};
let updateObj = {};
searchObj["username"] = uName;
updateObj["meals"] = mealObj;
let updateSched = col.findOneAndUpdate(searchObj, {$set: updateObj}, {
returnNewDocument: true,
upsert: true
}, (err, dbRes) => {
assert.equal(null, err);
res.send(true);
});
});
app.post('/signup', (req, res) => { //handles new user signup
let uName = req.body.username;
let passText = req.body.password;
let uObj = {}
uObj["username"] = uName;
uObj["meals"] = req.body.meals;
let userFree = col.find(uObj).toArray((err, results) =>{
if(err) return err;
let insertBool;
if(results.length > 0){
insertBool = false;
res.send({'openName': false});
}
else{
uObj['password'] = passText;
col.insertOne(uObj, (err, r) => {
if(err) return err;
assert.equal(1, r.insertedCount);
insertBool = true;
res.send({'openName': true});
});
}
});
});
app.listen(process.env.PORT || 5004, () =>
console.log('app is lisenting on port 5004')
);