Answers for "set proxy to use nodejs in angular"

1

set proxy to use nodejs in angular

// in NodeJS (3000 port) make:
app.get('/api/user', (req, res) => {
  res.json({});
});

// in Angular (4200 port):
// make call
async ngOnInit(): Promise<void> {
    let result = await this.http.get("http://127.0.0.1:4200/api/user").toPromise();

    console.log("Result: ", result);
    
}

// create a file (proxy.conf.json) in a root directory of Angular, which conteins:
{
    "/api": {
        "target": "http://localhost:3000",
        "secure": false
    }
}

// start Angular
ng serve --proxy-config proxy.conf.json
Posted by: Guest on July-29-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language