Peter Parker Peter Parker - 5 days ago 7
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

app.post('/api/auth/facebook/mobile', authenticationHandler.handleFacebookMobileLoginRequest);


Body parser for json is defined:

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


On the Angular client side, I do:

http({
method: 'POST',
url: 'http://192.168.1.101:3000/api/auth/facebook/mobile',
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
    data
    with
    params
    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
    .then(...
    function. Which again works well in other clients.



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

krl krl
Answer

Possible browser preflighted request CORS issue?

Try using CORS module in Node.js server:

var cors = require('cors')

app.use(cors());
Comments