Build a Simple Web App with Express & Angular
Last Updated :
17 Aug, 2024
Building a simple web app using Express and Angular is a great way to understand the fundamentals of full-stack development. Express, a minimalist web framework for Node.js, handles the backend, while Angular, a powerful front-end framework, provides the structure for the client-side application.
In this article, we’ll see the process of creating a basic web app that connects an Express backend with an Angular frontend.
Prerequisites
Before diving into the tutorial, make sure you have the following tools installed:
Steps To Setup and Build Web App
Step 1: Set up the project
First, we will create a new project directory and initialize it as an npm package. Open your terminal and navigate to the location where you want to create your project. Run the following command to create a new directory:
mkdir my-app
you can replace my-app with the name of your choice. Next, navigate to the new directory using:
cd my-app
 & initialize it as an npm package by running the following command:
npm init -y
This will create a package.json file in the root of your project, which will contain all the necessary information about your project and its dependencies.
Step 2: Install Angular & Express
Next, we will install angular and express for our project. Run the following command to install Angular and Express:
npm install @angular/cli express
Step 3: Create the Angular client
We will use the Angular CLI (Command Line Interface) to create the Angular client. Run the following command to generate a new Angular project:
ng new client
Project Structure

Folder Structure
Step 4: Create the Express server
Now, we will create the Express server. Create a new file called “server.js” at the root of your project. In this file, we will set up our Express server. First, we will update the Express server to handle requests from the Angular client. In the “server.js” file, add the following code to handle CORS (Cross-Origin Resource Sharing) and to allow the Angular client to make requests to the Express server:
JavaScript
//server.js
const express = require('express');
const app = express();
// handling CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin",
"http://localhost:4200");
res.header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept");
next();
});
// route for handling requests from the Angular client
app.get('/api/message', (req, res) => {
res.json({ message:
'Hello GEEKS FOR GEEKS Folks from the Express server!' });
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Step 5: Update the Angular client
Next, we will update the Angular client to make requests to the Express server. In the “src/app” directory, create a new file called “api.service.ts”. In this file, we will create a service that will handle the requests to the Express server.
JavaScript
//api.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient) { }
getMessage() {
return this.http.get(
'http://localhost:3000/api/message');
}
}
Here, we created a service that has a single method, “getMessage()”, which makes a GET request to the Express server’s ‘/api/message’ route. You will also require to import the HttpClient Module into your app.module.ts file, add the following code to import it:
JavaScript
//app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { AppRoutingModule }
from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule }
from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 6: Use the service in the Angular component
Finally, we will use the service in an Angular component to display the message from the Express server. In the “src/app” directory, open the “app.component.ts” file.
JavaScript
//app.component.ts
import { Component, OnInit } from '@angular/core';
import { ApiService } from './api.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'frontEnd';
message: any;
constructor(private apiService: ApiService) { };
ngOnInit() {
this.apiService.getMessage().subscribe(data => {
this.message = data;
});
}
}
Step 7: Display the message on the Angular template
Open the “src/app/app.component.html” file and add the following code to display the message from the server.
HTML
<!--app.component.html-->
<h1 style="color: green;
font-style: italic;">
GEEKS FOR GEEKS
</h1>
<h3>
Build a Simple Web App with Express & Angular
</h3>
<h5>
The below message is fetched from the express backend
</h5>
<p *ngIf="message">
{{ message.message }}
</p>
This will display the message from the Express server on the Angular template.
Step 8: Start the development server
Now that we have integrated the Angular front-end with the Express back-end, we can start the development server. To start the Angular development server, navigate into the “client” directory and run the following command:
ng serve
This will start the development server and make your Angular client available at “http://localhost:4200/”.
To start the Express server, open a new terminal window and navigate to the root of your project. Run the following command:
node server.js
With this, you have successfully created an example application that integrates the Angular front-end with the Express back-end. The Angular client makes a GET request to the Express server, which returns a JSON object containing a message. The message is then displayed on the Angular template.
Output: The following output will be displayed by the Angular and Express servers:
Express:
.gif)
Build a Simple Web App with Express & Angular
Angular:
.gif)
Build a Simple Web App with Express & Angular
Similar Reads
How to Create Todo List in Angular 7 ?
The ToDo app is used to help us to remember some important task. We just add the task and when accomplished, delete them. This to-do list uses various Bootstrap classes that make our web application not only attractive but also responsive. Approach: Create a new angular app using following command:
2 min read
Build a Simple Web App with Express & Angular
Building a simple web app using Express and Angular is a great way to understand the fundamentals of full-stack development. Express, a minimalist web framework for Node.js, handles the backend, while Angular, a powerful front-end framework, provides the structure for the client-side application. In
5 min read
How to build progressive web app(PWA) in Angular 9 ?
In this article, we will develop a PWA (Progressive Web App) using Angular. What is PWA ? Progressive Web Apps (PWAs) are web applications that have been designed so that they are capable, reliable, and installable. PWA are built and enhanced with modern APIs to deliver enhanced capabilities, reliab
7 min read
Routing in Angular 9/10
Routing in Angular allows the users to create a single-page application with multiple views and allows navigation between them. Users can switch between these views without losing the application state and properties. Approach: Create an Angular app that to be used.Create the navigation links inside
3 min read
How to create a To-Do list using Drag and Drop in Angular 7 ?
We can easily create a To-Do list using Drag-Drop module provided by angular Component Development Kit (CDK). First of all, create an angular component by using the following command- ng g c To-Do Now import CdkDragDrop, moveItemInArray, transferArrayItem from @angular/cdk/drag-drop to our to-Do com
2 min read
How to make a multi-select dropdown using Angular 11/10 ?
In this article, we will learn to build the multiple selection drop-down menu in Angular. To accomplish this task, we require Angular 10 or the Angular 11 version. Sometimes we need to display dynamically fetched multi-selected data in a drop-down menu, for this, we will use the npm @ng-select/ng-se
3 min read
How to set focus on input field automatically on page load in AngularJS ?
We can focus on any input field automatically using the angular directives. Here we create a custom directive that can auto-focus on any field in the form. Creating a custom directive is just like creating an Angular component. To create a custom directive we have to replace @Component decorator wit
3 min read
How to Scroll to an Element on click in Angular ?
In this article, we will see how to scroll to an element on click in Angular. Here, we will create a component that enables scrolling to specific targets when a button is pressed within the document from one target to another. Steps for Installing & Configuring the Angular ApplicationStep 1: Cre
4 min read
AngularJS $locationProvider
The $locationProvider facilitates the configuration of the application by implementing the deep linking paths that are stored. Here are some of the things that can be made with the $locationProvider service: Set the html5Mode property to true to enable HTML5 mode, which uses the history.pushState AP
4 min read
AngularJS $location Service
The $location in AngularJS basically uses a window.location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS. There are various methods in the $location s
4 min read