HTTP GET request with Node and Express (Nexmo)
I'm trying to handle inbound SMS from Nexmo and having trouble figuring it out. I'm relatively new to Node and Express as well. Their documentation shows they send out the request for a webhook to catch shown here. I've tried their sample code and added it to my routes file like this
var url = require('url');
module.exports = function(app) {
app.get('/webhook', function(request, response){
var url_parts = url.parse(request.url,true);
if (!url_parts.hasOwnProperty('to') || !url_parts.hasOwnProperty('msisdn') || !url_parts.hasOwnProperty('text'))
console.log('This is not an inbound message');
else {
//This is a DLR, check that your message has been delivered correctly
if (url_parts.hasOwnProperty('concat'))
{
console.log("Fail:" + url_parts.status + ": " + url_parts.err-code + ".n" );
}
else {
console.log("Success");
/*
* The following parameters in the delivery receipt should match the ones
* in your request:
* Request - from, dlr - ton
* Response - message-id, dlr - messageIdn
* Request - to, Responese - to, dlr - msisdnn
* Request - client-ref, dlr - client-refn
*/
}
}
response.writeHead(200, {"Content-Type": "text/html"});
response.end();
});
};
This is the file I render all of my pages and I did this '/webhook' page just to receive the inbound data. When I stick the callback url into nexmo it never confirms it making me think I'm probably not handling it correctly.
Their sample code here shows it in their http.createServer and I've got in my top level file called differently like this
require('./app/server/routes')(app);
if (app.get('env') == 'development') app.use(errorHandler());
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Should I be trying to catch it in here instead? Or should I be doing this on the client side? I've check out this question but I don't quite understand that, what would I put as the host, or is this not the right method at all? I also looked at this one and I thought the code I posted above pretty much did the same thing. Cheers for any help!
EDIT: I've got it working in a simple app like this:
var express = require('express');
var bodyParser = require('body-parser');
var url = require('url');
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/:pagename', function(req,res) {
var url_parts = url.parse(req.url,true);
console.log(url_parts.query.msisdn);
res.sendfile('public/' + req.params.pagename + '.html');
});
var port = process.env.PORT || 3333;
app.listen(port);
But I can't get this over to the full on app. I'm still unsure where I should be putting, I thought it would be in the router file but I never see a request from nexmo on my log files so I'm not sure whats going on there.
链接地址: http://www.djcxy.com/p/33092.html