下面是我的index.js文件的代码。我使用mongoose进行数据库连接。每次我尝试运行应用程序时,在get方法处显示错误。进程终止时出现没有子进程的错误。
`import express from 'express';
import mongoose from 'mongoose';
import bodyparser from 'body-parser';
import cors from 'cors';
import history from 'connect-history-api-fallback';
import routes from './routes/RecruitmentRoutes';
import routes1 from './routes/RecruitmentRoutes2';
import deleteRoutes from './routes/deleteRoutes'
var mongodb = require('mongodb');
var path = require('path');
const app = express();
//mongo connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb+srv://csci202:Olemiss@[email protected]/test?
retryWrites=true&w=majority',{
useNewUrlParser:true,
useUnifiedTopology: true
}).catch(err => console.log(err))
//bodyparser setup
app.use(bodyparser.urlencoded({extended:true}))
app.use(bodyparser.json());
app.use(cors());
app.use(history({verbose:true}))
const PORT = 4000;
routes(app);
routes1(app);
deleteRoutes(app);
app.use(express.static(path.join(__dirname,'build')));
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname),'build','index.html');
res.redirect('/');
})
app.listen(PORT,()=>{
console.log('Recruitment server running on port ')
})`
在Heroku中定义的端口存储在环境变量 process.env.PORT
. 你需要改变你的代码来使用它。怎么改?修改 PORT 变量的声明,而不是这样。
const PORT = 4000;
用这个。
const PORT = process.env.PORT || 4000
这一行使用的是 process.env.PORT
环境变量,如果它被定义了(也就是在Heroku上),并使用了 4000
如果没有定义(也就是说,在你的本地机器上)。