Running Angular 4 app using a JSON server

不羁的心 提交于 2020-01-02 07:09:44

问题


I'm trying to run an Angular 4 app while trying to use a JSON server that it has been coded with. The problem I'm having is that I don't understand how an Angular 4 app running on port 4200 can communicate with the JSON server on port 3000 at the same time. The app is an example of CRUD but when I try to add something, nothing gets posted.

This is my article.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);
    }
}

This is my db.json:

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

回答1:


I have backend service running on port 5005, and app running on 4200, in order to "talk" with each other I have set up proxy.config.json file which looks like this

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

and when I serve my app I run ng serve -open --sourcemap=false --proxy-config proxy.config.json command.

You can also try to do something like this.



来源:https://stackoverflow.com/questions/46158988/running-angular-4-app-using-a-json-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!