Issue
I have this MEAN Stack application and for the front-end I'm using Angular 6, I created the users rooting and back-end login system and I've tested it using Postman and it works as expected. but when I use the form I always get this error below:
I know the problem is to change the port from 4200
to 3000
but haven't found the right solution.
here is my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable()
export class UserService {
constructor(
private _httpClient: HttpClient
) { }
public auth(user) {
return this._httpClient.post('users/login', user).pipe(map((resp: any) => resp.json()));
}
}
and this is where I use the service:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { UserService } from '../../services/user.service';
@Component({
selector: 'app-side-menu',
templateUrl: './side-menu.component.html',
styleUrls: ['./side-menu.component.scss']
})
export class SideMenuComponent implements OnInit {
public positionsList: Array<any> = [];
public selectedOption: string;
public feedbackMessage: string;
public email: string;
public password: string;
public formNotValid: boolean;
constructor(
private _userService: UserService,
private _router: Router
) {
}
ngOnInit() {
}
public sendLoginForm() {
if(!this.email || !this.password){
this.formNotValid = true;
this.feedbackMessage = "Both fields are required to login!";
return false;
}
const user = {
email: this.email,
password: this.password
}
this._userService.auth(user).subscribe(resp => {
if(!resp.success){
this.feedbackMessage = resp.message;
return false;
}
});
}
public getSelectedOption(option) {
this.selectedOption = "as " + option;
}
}
Any idea will be appreciated.
Solution
You can just use the full URL in your call like this
this._httpClient.post(window.location.protocol + '//' + window.location.hostname + ':3000/' + 'users/login', user)
Ideally, you should have you microservices behind a reverse proxy and route your requests based on URI paths
Answered By - Pramodh Valavala
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.