我正在将服务器部署到Heroku,但是由于某种原因,网络显示它仍在向本地主机发出请求,而不是动态地将端口号注入到process.env.PORT
这是我的服务器的设置。
require('dotenv').config();
const express = require("express");
const graphqlHTTP = require("express-graphql");
const schema = require('./schema/schema');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
mongoose.connect(process.env.mongodburi, { useNewUrlParser: true })
mongoose.connection.once('open', ()=>{
console.log('Connected to database');
});
const app = express();
app.use(cors());
app.use(express.static("public"));
app.use('/graphql', bodyParser.json(), graphqlHTTP({
schema,
graphiql: true
}));
app.withCredentials = true;
app.use('/', (req, res) => res.send("Welcome to read my profile"));
const port = process.env.PORT;
app.listen(port, ()=>{
console.log(`Now listening requests on port:${port}`);
})
答案 0 :(得分:0)
使用此
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
或运行mentioned提到的$ heroku config:set PORT=3333