1

Let's say we have an Angular web app that lists patients, the route would be something like http://localhost:4200/patients.

Is there a way to deliver the same content (from the exact same data source) in JSON format (application/json) by calling http://localhost:4200/server/api/patients, for example?

In other words, I'd like to build a REST API endpoint with Angular.

EDIT: My goal is to provide a means for external systems to access real data in production environments.

5
  • 3
    You can use in memory web api. github.com/angular/in-memory-web-api Commented May 30, 2018 at 8:54
  • Your question in unclear : do you want to deliver real data ? Do you want to mock your data ? Do you want to use it in production or is it just for testing ? Please identify your use case and what you're expecting. Commented May 30, 2018 at 9:26
  • @trichetriche: See edit above. Commented May 30, 2018 at 11:28
  • Angular is primarily for Front End User Interface. There are many other technologies that lets you achieve your objective (For REST API End point creation) example. .Net Core. If you intende to do with scripting then go for Node.JS. Commented Aug 5, 2020 at 10:02
  • I wrote a way to do it - dev.to/jdgamble555/angular-universal-rest-api-endpoints-23fj Commented Apr 4, 2022 at 5:00

4 Answers 4

2

Angular apps normally run entirely in the browser. Since nothing is running on a server, it's not possible to use it for a web api.

However, I came across this question while implementing a small Angular Universal app with server-side rendering. In this case, an Express server does the SSR and I wanted it to also handle some api calls.

The server.ts file (autogenerated when adding @nguniversal/express-engine) includes a commented-out section showing how to do it.

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });

For testing, replace it with something like

  server.get('/api/**', (req, res) => { res.send('Hello!') });

You can hit it from the browser with http://localhost:4200/api/test, or from the angular app with this.http.get('api/test').

Sign up to request clarification or add additional context in comments.

Comments

1

Following your edit :

Yes you can. This is available in production mode, when you serve your application.

This isn't an Angular issue but a server issue : your server must

  1. Serve the built Angular application
  2. Serve the REST API.

This highly depends on your server, so you should make a new question with your server as tags, and ask how to set it up to serve several applications at once.

And I must state it because a lot of people don't get it, but Angular isn't made to create REST APIs. You will build your API with anything else you want (and that can do it), you will just serve it on the same domain.

Comments

0

if you want to prepend some string like 'server/api/', you can use the environtment to do that.

is this what you need?

Comments

0

This not possible because the Root component and sub component in angular is rendered using view templet which is html finally , Angular is javascript MVC Framework . So ultimately every data angular gets by making Web Service Api call should be populated into some component . So ultimately the mime type you will be getting back from angular is text/html .

Angular is not build for developing Rest Api . You can use Node.js for this purpose if you want to code only in javascript.

follow this link to know frameworks available in node.js for this purpose

Node js Rest Api frameworks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.