0

So, I have looked at some answers and will try to be as complete as I can as to what I have tried. Being a new fish here, I did look first but did not find a satisfactory answer.

First the problem. I am attempting to post id, password to an endpoint called obviously enough '/login' which is running on node. I have run the endpoint using postman and it does work.. I have tried with the httpOptions header set to json (thank you stackoverflow) but I am still getting the 404. I have for this purpose coding in the standard console logging to see where the flow is and it does not even get to the route.post..

Failed to load http://localhost:3000/login: Response for preflight has invalid HTTP status code 404.

First the angular side of things. As a service

public login(user, passwd) {
console.log('url %s user %s passwd %s', this.url, user, passwd);

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json'
  })
};

this.http.post(this.url, {
    id: user,
    password: passwd
  },
  httpOptions)

  .subscribe(
    res => {
      console.log(res);
    },
    err => {
      console.log('Error occured %o', err);
    }
  );
 }

Now the node code.. I placed a get here as well in case for some reason I was firing the wrong thing - i am not even getting the console logs.

app.route('/login')
.get(sessionChecker, (req, res) => {
    console.log ('login get');
    res.json({login : false});
    res.sendFile(__dirname + '/public/login.html');
})
.post((request, response) => {

    console.log("sesssion is %o", request.session);

    // taken from personal code I wrote a few years back
    console.log('request %o', request.query);
    let username = request.query.id;
    let password = request.query.password;

    console.log('user %s pass %s', username, password);

I do fall all the way through to my 404 url not found code.

// route for handling 404 requests(unavailable routes)
app.use(function (req, res, next) {
console.log(' some sort of error ');
res.status(404).send("Sorry can't find that!")
});

I get from here the message "Sorry Can't find that" which I would expect if I was giving the wrong url.

Alex Mackinnon
  • 300
  • 3
  • 12

1 Answers1

0

It turns out there is an answer for this and it is contained within the least voted on answer of 2 stackoverflow

A billion thank's to the guy. In a nutshell add the following to the server side CORS scripting..

if (req.method === "OPTIONS") 
    res.send(200);
else 
    next();
Alex Mackinnon
  • 300
  • 3
  • 12