【问题标题】:GET http://localhost:8000/api net::ERR_CONNECTION_REFUSEDGET http://localhost:8000/api net::ERR_CONNECTION_REFUSED
【发布时间】:2021-08-24 21:40:28
【问题描述】:

我正在制作一个 Angular 项目来显示、添加、删除和更新员工详细信息。我有一个让自己连接的 api,但是当我尝试发布或获取员工列表时,我总是遇到同样的错误。我正在使用 Angular、Express、Node 和 Mongodb。任何帮助将不胜感激。我不确定要放置什么代码以使帮助更容易,但我可以放置错误。在此处输入图像描述

Get Error

Error

Post Error

import { Injectable } from '@angular/core';
import { Employee } from './Employee';
import { catchError, map } from 'rxjs/operators';
import { Observable, throwError } from 'rxjs';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
 
@Injectable({
  providedIn: 'root'
})
 
export class CrudService {
 
  // Node/Express API
  REST_API: string = 'http://localhost:8000/api';
 
  // Http Header
  httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
  router: any;
 
  constructor(private httpClient: HttpClient) { }
 
  // Add
  AddEmployee(data: Employee): Observable<any> {
    let API_URL = `${this.REST_API}/add-employee`;
    return this.httpClient.post(API_URL, data)
      .pipe(
        catchError(this.handleError)
      )
  }
 
  // Get all objects
  GetEmployees() {
    return this.httpClient.get(`${this.REST_API}`);
  }
 
  // Get single object
  GetEmployee(id:any): Observable<any> {
    let API_URL = `${this.REST_API}/read-employee/${id}`;
    return this.httpClient.get(API_URL, { headers: this.httpHeaders })
      .pipe(map((res: any) => {
          return res || {}
        }),
        catchError(this.handleError)
      )
  }
 
  // Update
  updateEmployee(id:any, data:any): Observable<any> {
    let API_URL = `${this.REST_API}/update-employee/${id}`;
    return this.httpClient.put(API_URL, data, { headers: this.httpHeaders })
      .pipe(
        catchError(this.handleError)
      )
  }
 
  // Delete
  deleteEmployee(id:any): Observable<any> {
    let API_URL = `${this.REST_API}/delete-employee/${id}`;
    return this.httpClient.delete(API_URL, { headers: this.httpHeaders}).pipe(
        catchError(this.handleError)
      )
  }
 
 
  // Error 
  private handleError(error: Response | any) {
    let errorMessage = '';
    if (error.status == 0) {
      // Handle client error
      errorMessage = error.error.message;
      this.router.navigate(['/error'])
    } else {
      // Handle server error
      errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
    }
    console.log(errorMessage);
    return throwError(errorMessage);
  }
 
}

import { Injectable } from '@angular/core';
import { Employee } from './Employee';
import { catchError, map } from 'rxjs/operators';
import { Observable, throwError } from 'rxjs';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
 
@Injectable({
  providedIn: 'root'
})
 
export class CrudService {
 
  // Node/Express API
  REST_API: string = 'http://localhost:8000/api';
 
  // Http Header
  httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
  router: any;
 
  constructor(private httpClient: HttpClient) { }
 
  // Add
  AddEmployee(data: Employee): Observable<any> {
    let API_URL = `${this.REST_API}/add-employee`;
    return this.httpClient.post(API_URL, data)
      .pipe(
        catchError(this.handleError)
      )
  }
 
  // Get all objects
  GetEmployees() {
    return this.httpClient.get(`${this.REST_API}`);
  }
 
  // Get single object
  GetEmployee(id:any): Observable<any> {
    let API_URL = `${this.REST_API}/read-employee/${id}`;
    return this.httpClient.get(API_URL, { headers: this.httpHeaders })
      .pipe(map((res: any) => {
          return res || {}
        }),
        catchError(this.handleError)
      )
  }
 
  // Update
  updateEmployee(id:any, data:any): Observable<any> {
    let API_URL = `${this.REST_API}/update-employee/${id}`;
    return this.httpClient.put(API_URL, data, { headers: this.httpHeaders })
      .pipe(
        catchError(this.handleError)
      )
  }
 
  // Delete
  deleteEmployee(id:any): Observable<any> {
    let API_URL = `${this.REST_API}/delete-employee/${id}`;
    return this.httpClient.delete(API_URL, { headers: this.httpHeaders}).pipe(
        catchError(this.handleError)
      )
  }
 
 
  // Error 
  private handleError(error: Response | any) {
    let errorMessage = '';
    if (error.status == 0) {
      // Handle client error
      errorMessage = error.error.message;
      this.router.navigate(['/error'])
    } else {
      // Handle server error
      errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
    }
    console.log(errorMessage);
    return throwError(errorMessage);
  }
 
}

【问题讨论】:

  • 您的 express 服务器是否在 8000 端口运行?
  • 是的,我的 Express API 在 REST_API 上运行:string = 'localhost:8000/api。我已经编辑了我的帖子以显示 crud.service.ts
  • 尝试在浏览器中打开localhost:8000/api,看看会得到什么
  • 进入您的目录。并运行节点 NameOfFile.js。
  • 明白了,感谢一百万的帮助,我得到了它。我刚刚发布了另一个问题,您是否有机会看一看,看看您是否知道发生了什么?

标签: angular mongodb express


【解决方案1】:

我猜你的后端/快递服务器没有运行。可以看说明here 您可以使用node app.js 运行您的服务器 // 用你的文件名替换 app.js

因此,一旦您在浏览器上打开 http://localhost:8000/api,这应该会给出结果(因为这是一个 get 请求)。

【讨论】:

    【解决方案2】:
    1. 确保两台服务器都在运行(后端和前端)。

    2. 查看谷歌开发工具并查看网络部分。查看请求标头和一般信息。确保请求 URL / backend 有你的后端服务器 URL,而 orgin / frontend 有你的前端 URL。

    您的问题是您的后端没有正确连接到您的前端。

    【讨论】:

    • 我可以运行前端并且它正在工作,但我不确定如何启动后端或它们是否正确连接。我在哪里可以找到?
    • 尝试在您的后端运行 nodemon,它应该会显示您的端口号。例如本地主机:8000
    猜你喜欢
    • 2021-03-02
    • 2021-12-16
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2019-11-23
    相关资源
    最近更新 更多