简体   繁体   中英

Heroku failed to inject port number to process.env.PORT

I'm deploying my server to Heroku, but for some reason the network shows it keeps making request still to the localhost, instead of dynamically injecting a port number to process.env.PORT

chrome console error message

This is the setup of my server.

 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}`); }) 

Use this

app.listen(process.env.PORT || 3000, function(){
    console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

Or run $ heroku config:set PORT=3333 as mentioned by デビット

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM