0

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}`);
})
Bing Wang
  • 11
  • 2

1 Answers1

0

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 デビット

andrewjazbec
  • 859
  • 8
  • 20
  • The first option won't help. The issue isn't the address the _server_ is listening on, but rather the address the _client is requesting_. And the second option won't work. You can't pick your own port on Heroku. You _must_ use the port they give you. – ChrisGPT was on strike Feb 22 '19 at 13:32