【问题标题】:Running Angular 4 app using a JSON server使用 JSON 服务器运行 Angular 4 应用程序
【发布时间】:2018-02-19 21:20:47
【问题描述】:

我正在尝试运行 Angular 4 应用程序,同时尝试使用已编码的 JSON 服务器。我遇到的问题是我不明白在端口 4200 上运行的 Angular 4 应用程序如何同时与端口 3000 上的 JSON 服务器通信。该应用程序是 CRUD 的一个示例,但是当我尝试添加某些内容时,没有发布任何内容。

这是我的文章.service.ts:

@Injectable()
export class ArticleService {
    //URL for CRUD operations
    articleUrl = "http://localhost:3000/articles";
    //Create constructor to get Http instance
    constructor(private http:Http) { 
    }
    //Fetch all articles
    getAllArticles(): Observable<Article[]> {
        return this.http.get(this.articleUrl)
                .map(this.extractData)
                .catch(this.handleError);

    }
    //Create article
    createArticle(article: Article):Observable<number> {
        let cpHeaders = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: cpHeaders });
        return this.http.post(this.articleUrl, article, options)
               .map(success => success.status)
               .catch(this.handleError);
    }
    //Fetch article by id
    getArticleById(articleId: string): Observable<Article> {
        let cpHeaders = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: cpHeaders });
        console.log(this.articleUrl +"/"+ articleId);
        return this.http.get(this.articleUrl +"/"+ articleId)
               .map(this.extractData)
               .catch(this.handleError);
    }   
    //Update article
    updateArticle(article: Article):Observable<number> {
        let cpHeaders = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: cpHeaders });
        return this.http.put(this.articleUrl +"/"+ article.id, article, options)
               .map(success => success.status)
               .catch(this.handleError);
    }
    //Delete article    
    deleteArticleById(articleId: string): Observable<number> {
        let cpHeaders = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: cpHeaders });
        return this.http.delete(this.articleUrl +"/"+ articleId)
               .map(success => success.status)
               .catch(this.handleError);
    }   
    private extractData(res: Response) {
        let body = res.json();
        return body;
    }
    private handleError (error: Response | any) {
        console.error(error.message || error);
        return Observable.throw(error.status);
    }
}

这是我的 db.json:

{
    "articles": [
        {
        "id": 1,
        "title": "Android AsyncTask Example",
        "category": "Android"
        }
    ]
} 

【问题讨论】:

  • 请编辑您的问题并添加您使用服务的代码。

标签: json angular server


【解决方案1】:

我在端口 5005 上运行后端服务,在 4200 上运行应用程序,为了相互“交谈”,我设置了 proxy.config.json 文件,如下所示

{
  "/api/*":{
    "target":"http://localhost:5005",
    "secure": false,
    "logLevel": "debug"
  }
}

当我提供我的应用程序时,我会运行 ng serve -open --sourcemap=false --proxy-config proxy.config.json 命令。

你也可以尝试做这样的事情。

【讨论】:

  • 好吧,您的代码可以运行,但应用程序在创建对象时停止,所以我不确定它是否仍在连接到 JSON 服务器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多