Title: HTTP GET request with Node and Express (Nexmo)
Tags: javascript;node.js;express;vonage
Question: 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 - to\n
* Response - message-id, dlr - messageId\n
* Request - to, Responese - to, dlr - msisdn\n
* Request - client-ref, dlr - client-ref\n
*/
}
}
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.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:04/28/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><node.js><express><vonage>', 'Title': 'HTTP GET request with Node and Express (Nexmo)', 'ViewCount': 225, 'id': 36921857, 'user_AboutMe': None, 'user_AccountId': 8285016.0, 'user_DisplayName': 'Tri42', 'user_Reputation': 31.0, 'user_Views': 15.0, 'user_id': 6267804.0}}