Building a full-stack web application using Angular 19 as the frontend and Node.js with Express as the backend is a powerful approach. This combination allows developers to create scalable, efficient, and real-time applications. In this guide, we will walk through the steps to integrate Angular 19 with Node.js and Express, covering backend setup, frontend integration, and API communication.
mkdir backend && cd backend
npm init -y
2. Install required dependencies
npm install express cors body-parser mongoose dotenv
3. Create an Express server (server.js
)
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('/', (req, res) => {
res.send('Hello from Express');
});
app.listen(5000, () => {
console.log('Server running on port 5000');
});
4. Run the backend server
node server.js
ng new frontend
cd frontend
2. Install HttpClient module (if not already installed
npm install @angular/common
3. Update app.module.ts
to import HttpClientModule
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [HttpClientModule]
})
export class AppModule { }
ng generate service api
2. Modify api.service.ts
to fetch data from Express
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiUrl = 'http://localhost:5000';
constructor(private http: HttpClient) { }
getMessage() {
return this.http.get(`${this.apiUrl}`);
}
}
3. Use the service in a component (e.g., app.component.ts
)
import { Component, OnInit } from '@angular/core';
import { ApiService } from './api.service';
@Component({
selector: 'app-root',
template: `<h1>{{ message }}</h1>`
})
export class AppComponent implements OnInit {
message: string = '';
constructor(private apiService: ApiService) { }
ngOnInit() {
this.apiService.getMessage().subscribe((data: any) => {
this.message = data;
});
}
}
node server.js
2. Start the frontend
ng serve --open
3. Check in the browser
http://localhost:4200
and you should see the message from the Node.js backend.By following these steps, you have successfully integrated Angular 19 with Node.js and Express to create a full-stack web application. This setup allows seamless communication between the frontend and backend, enabling you to build modern, scalable, and interactive web applications. You can now extend this by adding database integration (MongoDB), authentication, and more complex APIs to create a fully functional web app. 🚀