2015-05-26 44 views
1

我正在從節點js後端接收用戶sms從android應用程序使用telerivet webhook API.everytime當我運行應用程序atheroku它給了我不能GET /錯誤,我的代碼index.js是無法在Heroku中獲取/錯誤節點js

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

var WEBHOOK_SECRET = "62DZWMCCFFHTTQ44CG3WUQ94CTT7GAAN"; 

app.post('/telerivet/webhook', 
bodyParser.urlencoded({ extended: true }), 
function(req, res) { 
    var secret = req.body.secret; 
    if (secret !== WEBHOOK_SECRET) { 
     res.status(403).end(); 
     return; 
    } 

    if (req.body.event == 'incoming_message') { 

    var content = req.body.content; 
    var from_number = req.body.from_number; 
    var phone_id = req.body.phone_id; 

    // do something with the message, e.g. send an autoreply 
    res.json({ 
     messages: [ 
     { content: "Thanks for your message!,Our Backend Is Still in Alpha Stage,Hang Tight" } 
     ] 
    }); 

    } 

    res.status(200).end(); 
} 
); 

app.listen(process.env.PORT || 5000); 

請大家幫忙,在那裏IM我做錯了 它給人當我運行在瀏覽器應用程序我不能GET /錯誤,也犯規與應用

+0

您正在請求一個根或「主頁」頁面,但尚未定義一個 - 'app.get('/',function ...);'。 –

+0

可以詳細說明嗎?如果可能的話添加代碼?請 – SheikhZayed

+1

當您嘗試訪問'http:// your-app.herokuapp.com /'時,您的瀏覽器會發送'GET'類型的請求和'/'路徑。但是,您的應用程序尚未定義如何響應此類請求,因此Express會以[404 Not Found](https://en.wikipedia.org/wiki/HTTP_404)進行響應。您可以使用['app.get()'來響應'GET'請求](http://expressjs.com/4x/api.html#app.METHOD)。一個簡單的例子是'app.get('/',function(req,res){res.send('Hello');});'。有關更多信息,請參閱[Express'路由指南](http://expressjs.com/guide/routing.html)。 –

回答

0

測試時需要短信回覆我找到了出路,這是我的新代碼:

var express = require('express'); 
    var app = express(); 

app.set('port', (process.env.PORT || 5000)); 
app.use(express.static(__dirname + '/public')); 

app.get('/', function(request, response) { 
response.send('Hello World!'); 
}); 

var bodyParser = require('body-parser'); 

var WEBHOOK_SECRET = "62DZWMCCFFHTTQ44CG3WUQ94CTT7GAAN"; 

app.post('/telerivet/webhook', bodyParser.urlencoded({ extended: true  }),function(req, res) { 
    var secret = req.body.secret; 
    if (secret !== WEBHOOK_SECRET) { 
     res.status(403).end(); 
     return; 
    } 

    if (req.body.event == 'incoming_message') { 

     var content = req.body.content; 
     var from_number = req.body.from_number; 
     var phone_id = req.body.phone_id; 

    // do something with the message, e.g. send an autoreply 
     res.json({ 
     messages: [ 
     { content: "Thanks for your message!,Stay Tuned for Awesome " } 
     ] 
    }); 

    } 

    res.status(200).end(); 
    } 
    ); 

     app.listen(app.get('port'), function() { 
     console.log('Node app is running on port', app.get('port')); 
     });