简体   繁体   中英

Heroku - specific route works locally but not on production

I have a node project which was deployed to an Heroku app. The deploy was successful and i can open the app , but there is one route to which i'm getting a "Cannot GET" error (404) while other routes on the same page are working as expected. Locally everything is working as expected and when i run heroku local in cmd i can see the response coming back from that function but i can't say the same for trying it from heroku app link.

server.js

'use strict';
var http = require ('http');    
var url = require('url') ;
var express= require('express');
var app= express();
var port = process.env.PORT || 3000;
var mongoose = require ('mongoose');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
mongoose.connect (db_details);
var conn=mongoose.connection;

var trip = require ('./Schemas/trip');
var user = require ('./Schemas/user');
app.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type,accept,access_token,X-Requested-With');
next();
});

conn.on('error', function(err) {
console.log('connection error: ' + err);
 process.exit(1);
 });

conn.once('open',function() {
 console.log('connected successfuly to the remote DB');

app.use(require('./Routes')); //API routings


app.listen(port);
console.log("listening on port "+port+" and waiting for WS requests");

 });

Routes/api/trip.js

'use strict'
var router = require('express').Router();
var mongoose = require('mongoose');
var trip = require ('../../Schemas/trip');
var user = require ('../../Schemas/user');
var Gmap = require ('../../TripBuilder/builder');

// get all trips
router.get('/getTrips', function(req, res) {
trip.find({},'trip_id Trip_Name Country City', function(err, trips) {
    res.send(trips.reduce(function(userMap, item) {
        userMap[item.id] = item;
        return userMap;
    }, {}));
});
});

// create new trip
router.post('/addNewTrip', function(req, res,next) {
 let newTrip = new trip ({"Trip_Id":req.body.Trip_id,"Trip_Name":req.body.Trip_Name,"Trip_Date":req.body.Trip_Date,
"Trip_Owner":req.body.Trip_Owner,
    "Country":req.body.Country,"City":req.body.City,"status":"Pending","Days":[],"Sites":[]});

 return newTrip.save().then(function(){

  return res.send("A Trip was created");

}).catch(next);
});

router.post('/addUserToTrip', async function(req, res,next) {
user.find({'email':req.body.email},'first_name last_name email',     function(err,obj) {console.log("print " +obj);  });
 let secUser = {"Trip_Id":req.body.Trip_id};
});

router.post('/createRoute', function(req, res,next) {
var map=new Gmap();
var origins = ['Big Ben, London, UK','Bridge St, Westminster, London SW1A 2JR, UK','Palace of Westminster, Westminster, London SW1A 0PW, UK','Whitehall, Westminster, London SW1A 2ET, UK'];
var destinations =['Big Ben, London, UK','Bridge St, Westminster, London SW1A 2JR, UK','Palace of Westminster, Westminster, London SW1A 0PW, UK','Whitehall, Westminster, London SW1A 2ET, UK'];
map.calcRoute(origins,destinations).then(function(result){
map.longestroute=result; //save start and end
origins.splice(origins.indexOf( map.longestroute.origin), 1);
origins.splice(origins.indexOf( map.longestroute.destination), 1);
map.waypoints=origins;
      map.setRoute(map.longestroute.origin,map.longestroute.destination,map.waypoints).then(function(route){


  return res.send(route);

  });

}).catch(next);
});


module.exports = router;    

Calling https://APP-NAME.herokuapp.com/api/trip/createRoute returns "Cannot GET /api/trip/createRoute", while calling https://APP-NAME.herokuapp.com/api/trip/getTrips returns a response.

Heroku logs seems to record the request without any special exceptions but nothing is coming back.

I added "res.send("ok");" inside "createRoute" just to see at least that will be sent back but nothing.

Package.json

"name": "tripin",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"engines": {
"node": "9.8.0"
},
"author": "",
"license": "ISC",
"dependencies": {
"@google/maps": "^0.4.6",
"body-parser": "^1.18.2",
"express": "^4.16.2",
"http": "0.0.0",
"https": "^1.0.0",
"inspect-process": "^0.5.0",
"mongoose": "^5.0.9",
"node-dev": "^3.1.3",
"package.json": "^2.0.1",
"request": "^2.85.0"
}
}

Thanks

Edit: router.post('/createRoute) and router.get('/createRoute) were attempted. didn't work in either case

Your error messages is complaining about the GET, so it wants to GET "createRoute". The create route path is a POST (so your HTTP request should be a POST). You can check this by implementing a GET, and give a response your will reconize..

router.get('/createRoute', function(req, res) {  
   // .. your reconizalbe response here ..
 res.send("Oops I did a GET, but wanted to POST");
}

and/or test with tool which can invoke the POST, like Postman or some other tool.

Ok , so as we all know programming rules state that the smaller the issue is, the harder it is to find it.

I managed to find out the solution: I had my project duplicated in heroku - once in the root folder and once in its folder like it should be. That threw off any routing i tried. cleaning up heroku and re-deploy did the trick.

Thank you all for trying to help

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