Search code examples
angularproxyemail-validation

Angular proxy got error when I put email in payload


I use Angular developing reset password module, I need send email to backend to validate it first. I use proxy to send API request to backend, following is my proxy setting:

"/api": {
    "target": "http://localhost:3000",
    "secure": false,
    "changeOrigin": true
  }

Then I found this API request always got following error, even backend already received this email verification request and finished processing. (ONLY this api request, others work fine) Can someone help on solution on this?

[HPM] Error occurred while trying to proxy request /api/reader/verifyemail from localhost:4200 to http://localhost:3000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Following is my component code - call API part:

submitEmail() {
    const val = this.email.value; //email value from input element
    this.readerService.verifyEmail({ email: val }).subscribe((data) => {
      if (data) {
        this.logger.info(`Email validation success for reader ${data}`);
        this.readerService.sendResetEmail(val).subscribe((data) => {
          if (data) {
            window.alert('Please check your email and follow it to reset the password');
            this.router.navigateByUrl('/reader/login');
          }
        })
      } else {
        this.logger.warn('Email validation failed');
        window.alert('No account is associated with this email')
      }
    })
  }

Following is my API service in frontend:

verifyEmail(input: emailDto): Observable<any> {
    return this.http.post('/api/reader/verifyemail', input).pipe(
      catchError(this.handleError('verifyEmail')), shareReplay()
    )
  }

Backend API part should be fine, because I tested everything including sending reset email all work fine. Their return value either json object or null, like my other APIs. Thank you very much!


Solution

  • I found the reason, because my html file issue, I use FormBuild instead of FormControls to redefine the form, the error disappeared.