Deploying MongoDB & Express based back-end API to Heroku dosen't work












0















I wrote a small front-end project with Vue.js, and Express was adopted in the back-end. The front-end and back-end were in two different directories respectively. The API works in my local environment but when I deploy my back-end api to Heroku, and tried GET request by Postman, it doesn't work.



In my index.js:



This is the listening port:
const port = process.env.PORT || 5000;



I am using mongoose to connect mLab:
const db = require("./config/keys").mongoURI;



mongoose.connect(db)



And my package.jsonis here:



{
"name": "backend-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node index.js",
"server": "nodemon index.js"
},
"author": "Yeoman_Li",
"license": "ISC",
"devDependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.0"
}
}


My index.js is here:



const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

// CORS
app.all("*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, X-Access-Token, X-Key"
);
if ("OPTIONS" == req.method) res.status(200).end();
else next();
});

const mediaReq = require("./routes/api/mediaReq");

// DB
const db = require("./config/keys").mongoURI;

// body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// connect to DB
mongoose
.connect(db)
.then(() => console.log("MonogoDB Connected"))
.catch(err => console.log(err));

// router
app.use("/api/mediaReq", mediaReq);

// port
const port = process.env.PORT || 5000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});


And here is my api router:



const express = require("express");
const router = express.Router();

const mediaController = require("../../controllers/media");

// test
router.get("/test", (req, res) => {
res.json({ msg: "test works" });
});

// $route GET api/mediaReq/medias || /medias/:id
router.get("/medias", mediaController.allMedia);
router.get("/medias/:id", mediaController.byId);
router.post("/medias", mediaController.create);
router.delete("/medias/:id", mediaController.remove);

module.exports = router;


I used postman to send a simple GET request and it works:
http://localhost:5000/api/mediaReq/medias



But when I tried to send a GET request to my heroku backend-api, it doesn't work:
https://fast-reaches-23458.herokuapp.com/api/mediaReq/medias










share|improve this question

























  • Server should be listening on port 80 for your request

    – Deathmras
    Dec 29 '18 at 2:59











  • You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

    – Yeoman_Li
    Dec 29 '18 at 3:12











  • In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

    – Deathmras
    Dec 29 '18 at 3:18


















0















I wrote a small front-end project with Vue.js, and Express was adopted in the back-end. The front-end and back-end were in two different directories respectively. The API works in my local environment but when I deploy my back-end api to Heroku, and tried GET request by Postman, it doesn't work.



In my index.js:



This is the listening port:
const port = process.env.PORT || 5000;



I am using mongoose to connect mLab:
const db = require("./config/keys").mongoURI;



mongoose.connect(db)



And my package.jsonis here:



{
"name": "backend-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node index.js",
"server": "nodemon index.js"
},
"author": "Yeoman_Li",
"license": "ISC",
"devDependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.0"
}
}


My index.js is here:



const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

// CORS
app.all("*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, X-Access-Token, X-Key"
);
if ("OPTIONS" == req.method) res.status(200).end();
else next();
});

const mediaReq = require("./routes/api/mediaReq");

// DB
const db = require("./config/keys").mongoURI;

// body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// connect to DB
mongoose
.connect(db)
.then(() => console.log("MonogoDB Connected"))
.catch(err => console.log(err));

// router
app.use("/api/mediaReq", mediaReq);

// port
const port = process.env.PORT || 5000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});


And here is my api router:



const express = require("express");
const router = express.Router();

const mediaController = require("../../controllers/media");

// test
router.get("/test", (req, res) => {
res.json({ msg: "test works" });
});

// $route GET api/mediaReq/medias || /medias/:id
router.get("/medias", mediaController.allMedia);
router.get("/medias/:id", mediaController.byId);
router.post("/medias", mediaController.create);
router.delete("/medias/:id", mediaController.remove);

module.exports = router;


I used postman to send a simple GET request and it works:
http://localhost:5000/api/mediaReq/medias



But when I tried to send a GET request to my heroku backend-api, it doesn't work:
https://fast-reaches-23458.herokuapp.com/api/mediaReq/medias










share|improve this question

























  • Server should be listening on port 80 for your request

    – Deathmras
    Dec 29 '18 at 2:59











  • You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

    – Yeoman_Li
    Dec 29 '18 at 3:12











  • In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

    – Deathmras
    Dec 29 '18 at 3:18
















0












0








0








I wrote a small front-end project with Vue.js, and Express was adopted in the back-end. The front-end and back-end were in two different directories respectively. The API works in my local environment but when I deploy my back-end api to Heroku, and tried GET request by Postman, it doesn't work.



In my index.js:



This is the listening port:
const port = process.env.PORT || 5000;



I am using mongoose to connect mLab:
const db = require("./config/keys").mongoURI;



mongoose.connect(db)



And my package.jsonis here:



{
"name": "backend-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node index.js",
"server": "nodemon index.js"
},
"author": "Yeoman_Li",
"license": "ISC",
"devDependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.0"
}
}


My index.js is here:



const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

// CORS
app.all("*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, X-Access-Token, X-Key"
);
if ("OPTIONS" == req.method) res.status(200).end();
else next();
});

const mediaReq = require("./routes/api/mediaReq");

// DB
const db = require("./config/keys").mongoURI;

// body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// connect to DB
mongoose
.connect(db)
.then(() => console.log("MonogoDB Connected"))
.catch(err => console.log(err));

// router
app.use("/api/mediaReq", mediaReq);

// port
const port = process.env.PORT || 5000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});


And here is my api router:



const express = require("express");
const router = express.Router();

const mediaController = require("../../controllers/media");

// test
router.get("/test", (req, res) => {
res.json({ msg: "test works" });
});

// $route GET api/mediaReq/medias || /medias/:id
router.get("/medias", mediaController.allMedia);
router.get("/medias/:id", mediaController.byId);
router.post("/medias", mediaController.create);
router.delete("/medias/:id", mediaController.remove);

module.exports = router;


I used postman to send a simple GET request and it works:
http://localhost:5000/api/mediaReq/medias



But when I tried to send a GET request to my heroku backend-api, it doesn't work:
https://fast-reaches-23458.herokuapp.com/api/mediaReq/medias










share|improve this question
















I wrote a small front-end project with Vue.js, and Express was adopted in the back-end. The front-end and back-end were in two different directories respectively. The API works in my local environment but when I deploy my back-end api to Heroku, and tried GET request by Postman, it doesn't work.



In my index.js:



This is the listening port:
const port = process.env.PORT || 5000;



I am using mongoose to connect mLab:
const db = require("./config/keys").mongoURI;



mongoose.connect(db)



And my package.jsonis here:



{
"name": "backend-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node index.js",
"server": "nodemon index.js"
},
"author": "Yeoman_Li",
"license": "ISC",
"devDependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.0"
}
}


My index.js is here:



const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");

const app = express();

// CORS
app.all("*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, X-Access-Token, X-Key"
);
if ("OPTIONS" == req.method) res.status(200).end();
else next();
});

const mediaReq = require("./routes/api/mediaReq");

// DB
const db = require("./config/keys").mongoURI;

// body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// connect to DB
mongoose
.connect(db)
.then(() => console.log("MonogoDB Connected"))
.catch(err => console.log(err));

// router
app.use("/api/mediaReq", mediaReq);

// port
const port = process.env.PORT || 5000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});


And here is my api router:



const express = require("express");
const router = express.Router();

const mediaController = require("../../controllers/media");

// test
router.get("/test", (req, res) => {
res.json({ msg: "test works" });
});

// $route GET api/mediaReq/medias || /medias/:id
router.get("/medias", mediaController.allMedia);
router.get("/medias/:id", mediaController.byId);
router.post("/medias", mediaController.create);
router.delete("/medias/:id", mediaController.remove);

module.exports = router;


I used postman to send a simple GET request and it works:
http://localhost:5000/api/mediaReq/medias



But when I tried to send a GET request to my heroku backend-api, it doesn't work:
https://fast-reaches-23458.herokuapp.com/api/mediaReq/medias







node.js mongodb express heroku






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 3:18







Yeoman_Li

















asked Dec 29 '18 at 2:54









Yeoman_LiYeoman_Li

187




187













  • Server should be listening on port 80 for your request

    – Deathmras
    Dec 29 '18 at 2:59











  • You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

    – Yeoman_Li
    Dec 29 '18 at 3:12











  • In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

    – Deathmras
    Dec 29 '18 at 3:18





















  • Server should be listening on port 80 for your request

    – Deathmras
    Dec 29 '18 at 2:59











  • You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

    – Yeoman_Li
    Dec 29 '18 at 3:12











  • In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

    – Deathmras
    Dec 29 '18 at 3:18



















Server should be listening on port 80 for your request

– Deathmras
Dec 29 '18 at 2:59





Server should be listening on port 80 for your request

– Deathmras
Dec 29 '18 at 2:59













You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

– Yeoman_Li
Dec 29 '18 at 3:12





You mean I have to change my index.js like this: const port = 80 or just change my request statement like this: https://fast-reaches-23458.herokuapp.com:80/api/mediaReq/medias ?

– Yeoman_Li
Dec 29 '18 at 3:12













In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

– Deathmras
Dec 29 '18 at 3:18







In index.js, your express server should listening to port 80, that's standard port for HTTP. I assume you'r not configure your virtual machine in different way, also try without https, just http

– Deathmras
Dec 29 '18 at 3:18














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966321%2fdeploying-mongodb-express-based-back-end-api-to-heroku-dosent-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966321%2fdeploying-mongodb-express-based-back-end-api-to-heroku-dosent-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas