How to create an API in Angular

0 votes
Can i know How to create an API in Angular?
Feb 23 in Angular by Ashutosh
• 19,190 points
37 views

1 answer to this question.

0 votes

Angular itself does not create APIs, but it can consume APIs. However, if you want to build a backend API for an Angular app, you can use Node.js with Express.js.

1. Set Up Node.js and Express

Install Node.js if not already installed.

Create a new folder and initialize a project:

mkdir my-api && cd my-api

npm init -y

npm install express cors body-parser mongoose

2. Create server.js (Main API File)

const express = require("express");

const cors = require("cors");

const bodyParser = require("body-parser");

const app = express();

app.use(cors());

app.use(bodyParser.json());

app.get("/api/message", (req, res) => {

  res.json({ message: "Hello from API!" });

});

const PORT = 5000;

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

3. Run the API Server

node server.js

answered Feb 24 by Kavya

Related Questions In Angular

0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,138 views
0 votes
1 answer

How to set a default base URL for all API calls in Angular?

In Angular, you can set a default ...READ MORE

answered Feb 26 in Angular by Kavya
36 views
0 votes
0 answers

How to lazy load module after api response in angular?

Can someone exlpain me with the code ...READ MORE

3 days ago in Angular by Nidhi
• 10,860 points
22 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to change the value of an Observable in TypeScript Angular?

To change the value of an Observable ...READ MORE

answered Feb 21 in Angular by Kavya
117 views
0 votes
0 answers

How to create currency pipe in Angular?

Can you help me with a code ...READ MORE

6 days ago in Angular by Nidhi
• 10,860 points
21 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP