Peter Parker Peter Parker - 5 months ago 36
Node.js Question

Node/Express not finding route for Angular http json post (404)

I'm developing a new Angular client which should communicate with my Node/Express server. I'm currently trying to develop the first step aka the login. This should be an http json post. It turns out that every single time I execute that post from the client to the server, the Node/Express server doesn't find the route for my path.

On the console log of the server, for my post json request I find following stacktrace:

OPTIONS /api/auth/facebook/mobile 404 274.092 ms - 1980

Node/Express side:

My route'/api/auth/facebook/mobile', authenticationHandler.handleFacebookMobileLoginRequest);

Body parser for json is defined:

app.use(bodyParser.urlencoded({ extended: true }));

On the Angular client side, I do:

method: 'POST',
url: '',
headers: { 'Content-Type': 'application/json; charset=UTF-8;' },
data: {fbToken: authResponse.accessToken}
.then(function (response) {

Fun facts:

  1. Same route works fine when I call it from a Java app or and Android native app, like

    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SERVER_URL + "/api/auth/facebook/mobile", params, jsonRequestListener, errorListener);

  2. On my Angular client, when I change
    then it works, the route is found...but I mean that doesn't make any sense. Moreover, I've got then a problem on the Angular client side, the answer isn't processed respectively the answer never land in the
    function. Which again works well in other clients.

Anyone got an idea, a clue or should I call Dr. Strange?

krl krl

Possible browser preflighted request CORS issue?

Try using CORS module in Node.js server:

var cors = require('cors')