Heroku特定的路线在本地工作但不在生产中

时间:2018-04-14 10:16:17

标签: javascript node.js express heroku

我有一个部署到Heroku应用程序的节点项目。 部署成功,我可以打开应用程序,但有一条路线,我得到一个“无法获取”错误(404),而同一页面上的其他路线正在按预期工作。 本地一切都按预期工作,当我在cmd中运行heroku本地时,我可以看到从该函数返回的响应,但我不能说从heroku应用程序链接尝试它。

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");

 });

路线/ 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;    

调用https://APP-NAME.herokuapp.com/api/trip/createRoute会返回“无法GET / api / trip / createRoute”,而调用https://APP-NAME.herokuapp.com/api/trip/getTrips会返回响应。

Heroku日志似乎记录了请求,没有任何特殊例外,但没有任何回复。

我添加了“res.send(”ok“);”在“createRoute”里面只是为了看到至少会发回但没有。

的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"
}
}

由于

修改 尝试了router.post('/ createRoute)和router.get('/ createRoute)。 在任何一种情况下都不起作用

2 个答案:

答案 0 :(得分:1)

您的错误消息是抱怨GET,所以它想要GET" createRoute"。 创建路由路径是POST(因此您的HTTP请求应该是POST)。 您可以通过实施GET来检查这一点,并给出一个您将重新协商的回复..

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

和/或使用可以调用POST的工具进行测试,如Postman或其他工具。

答案 1 :(得分:0)

好的,我们都知道编程规则表明问题越小,找到它就越难。

我设法找到了解决方案: 我的项目在heroku中重复了 - 一次在根文件夹中,一次在它的文件夹中就像它应该的那样。 抛弃了我试过的任何路由。 清理heroku并重新部署就可以了。

谢谢大家帮忙