0

I'm new to Angular 2/4/5 (migrating from AngularJS). I'm running a NodeJs + Express server at:

http://localhost:3000

I'm running my Angular project as:

ng serve --open

which runs as:

http://localhost:4200

I created HttpService with following function to make http requests:

sendRequest(requestObject): Promise<any> {
    // set created_at / updated_at on request if create or update operation
    if (this.isDataUpdateOperation(requestObject)) {
        requestObject = this.setDate(requestObject);
    }

    let headers = new Headers({ 'Content-Type': 'application/json', 'withCredentials': true });
    let options = new RequestOptions({ headers: headers });

    if(requestObject.REQUEST_METHOD == this.constants.REQUEST_METHODS.GET){
        return this.http.post(
            this.constants.SERVER.ROOT + this.constants.SERVER.API_PATH + requestObject.API_URL,
            requestObject.REQUEST_DATA,
            options).toPromise()
            .then((response)=>{
                return response.json();
            });
    }
    else if(requestObject.REQUEST_METHOD == this.constants.REQUEST_METHODS.POST){
        return this.http.post(
            this.constants.SERVER.ROOT + this.constants.SERVER.API_PATH + requestObject.API_URL,
            requestObject.REQUEST_DATA,
            options).toPromise()
            .then((response) => {
                return response.json();
            });
    }
    else if(requestObject.REQUEST_METHOD == this.constants.REQUEST_METHODS.PUT){
        return this.http.put(
            this.constants.SERVER.ROOT + this.constants.SERVER.API_PATH + requestObject.API_URL,
            requestObject.REQUEST_DATA,
            options).toPromise()
            .then((response) => {
                return response.json();
            });
    }
    else if(requestObject.REQUEST_METHOD == this.constants.REQUEST_METHODS.DELETE){
        return this.http.delete(
            this.constants.SERVER.ROOT + this.constants.SERVER.API_PATH + requestObject.API_URL,
            options).toPromise()
            .then((response) => {
                return response.json();
            });
    }
}

After login, all my calls to server failed with 403 response code and with message:

Response for preflight has invalid HTTP status code 403

I checked the response and found following response:

[
  {
    "message": "NOT LOGGED"
  }
]

What I guessed that it could be CORS issue and the session is not either stored or returned back to server.

I also used chrome CORS plugin but of no help.

The last thing I can think of is to build Angular project and copy the code to node project but this will make my development slow.

What am I doing wrong?

Ashutosh
  • 4,371
  • 10
  • 59
  • 105

0 Answers0