Varun Vu
Varun Vu

Reputation: 315

Heroku CANNOT /GET

I am trying to deploy my Node.js Api to Heroku, but after I deploy it, I get Cannot GET / on the APPNAME.herokuapp.com

Here is my app.js file.

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname + '/public'));


app.use('/api', require('./routes/api'));

var server = app.listen(process.env.PORT || 3000, function() {
    var port = server.address().port;
    console.log("Hello World");
});

This works locally when I use port 3000, but I feel like the issue may be pertained to the ports not being configured for production.

Does anybody know what i'm doing wrong? Any help would be greatly appreciated!

Upvotes: 1

Views: 7568

Answers (3)

Gabriel Obafisile
Gabriel Obafisile

Reputation: 29

Heroku needs to look for an index route when loading your app. This is what works for me and I think it will for you too.

app.get('/', (req, res, next) => {

    res.status(200).json({
        status: 'success',
        data: {
            name: 'name of your app',
            version: '0.1.0'
        }
    });

});

Upvotes: 0

Abimbola Bamgbelu
Abimbola Bamgbelu

Reputation: 21

You need to have an index route app.get("/"). Heroku will look for an index route to display and once it cannot find it, it will display the error message Cannot Get /. Meaning it cannot get the index route "/". In your code, you seem to have app.use('/api', require('./routes/api'));. I am assuming you intend this to be your index page, but this isn't how it works. The index page is only the backstroke "/".

Upvotes: 1

QauseenMZ
QauseenMZ

Reputation: 1251

Adding these two lines in server.js worked for me:

var distDir = __dirname + "/dist/"; app.use(express.static(distDir));

My dist structure is as follows:

enter image description here

Upvotes: 0

Related Questions