node js express cannot post /

Hello APIGEEKS,

I'm making a call to nodejs app using static URL with no proxy suffix path(since due to client requirment ).

Let's consider below sample code.The call to the nodejs app is working if the request is made with complete URL (ex: http://kaprthikprabhu-prod.apigee.net/testnodejs/placeOrder ) ,and fails when call made with URL no suffix path ( ex: http://kaprthikprabhu-prod.apigee.net/testnodejs ) .

However, later modified request by including suffix path using assign message policy but yet the request to nodejs app fails.I had played with it by placing this policy at different places of flow request but i got response from nodeapp has "Cannot POST /" .

For above both cases,the request from target end point to node app is relative and could see the same in trace but the call fails for second case.

Could you please let me know why the calling is getting failed even after including suffix path.Or Does node app executes only for complete URL ? Please share your thoughts on this.

var express = require('express');
var app = express();
console.log('node.js application starting...');

app.post('/placeOrder', function(req, res) {
   
    console.log("post received");
});

app.listen(9003, function() {
    console.log('Node HTTP server is listening');
});
0 6 25.7K
6 REPLIES 6

Could any experts assit me on this.

Thanks,KP

Not applicable

Hi @Karthik Prabhu,

I think you would have to have your node JS function signature changed to app.post('/',function(request, response) ... for your call to work without suffix.

Please do let us know if resolves the issue.

Hello @jpprakash,

Yes, I had done this before and works.Now i have a case where nodejs functions should be called based on route condition and same included in request path suffix in proxy/target request flow, but this dont invokes nodejs code.

PS: i haven't created multiple target endpoint to call separate node app.

Thanks.,

Karthik


Cool, In which case could you try using a switch case inside your node js function and switch with URL path.

a pseudo code shown below..

app.post('/',function(request, response)

{

switch (req.url) { --> you may use some string manipulation to strip out the other parts of URL if needed

case 'case1':

return function()

{

return /* code */;

}();

case 'case2':

return function() {

return /* code */;

}();

case 'case3':

return function() {

return /* code */;

}(); ...

default:

return function() {

return /* code */;

}();

}

Thanks @jpprakash will do try this out.

thank you @Karthik Prabhu!