Building a weather app with the MEAN Stack: A step-by-step guide for beginners
Introduction:
In this Article, You will go through the process of building a weather app using the MEAN stack. The MEAN stack is MongoDB, Express.js, Angular and Node.js. including technology. By following this comprehensive guide, beginners can learn how to combine these technologies to create a weather app that captures and displays weather data in real-time.
Prerequisites:
Before you begin, make sure the following conditions are met:
- Basic knowledge of HTML, CSS and JavaScript
- Introduction to MEAN stack technologies: MongoDB, Express.js, Angular and Node.js
- Your favorite text editor (eg Visual Studio Code)
- Node.js and npm (Node Package Manager) installed on your device
Step 1: Setup the project
- Create a new directory for your project and navigate to it using the command line.
- Start a new Node.js project by running the "npm init -y" command.
- Install the required dependencies by running the following command:
- Express.js: `npm Express Express'
- Mongoose (for MongoDB integration): "npm mongoose"
- Request (to make API requests): "npm install request"
Step 2: Create a backend server
- Build the Express.js server by creating a new file called "server.js".
- Import the necessary modules:
const Express = request ( 'Express' );
const program = express();
const request = request('request');
const mongoose = require('mongoose');
- Connect to MongoDB using Mongoose:
mongoose.connect('mongodb://localhost/air-app', {
useNewUrlParser: true
useUnifiedTopology: true
});
- Create a weather scheme and model to store weather data:
`
const schema = new mongoose. Schema({
city: line
temperature: Amount
moisture: amount,
Wind speed: Num
});
const Weather = mongoose.model('Weather', weatherSchema);
`
- Run the API endpoint to get the weather data:
`
app.get('/api/weather/:city', (req, res) => {
const apiKey = 'YOUR_API_KEY';
const city = req.params.city;
const url = http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${city}
;
request (url, (error, response, body) => {
if (!error && response.statusCode === 200) {
const data = JSON.parse(body);
const weather = new Weather({
city: data.location.name,
temperature: data.current.temp_c,
humidity: data.current.humidity,
windSpeed: data.current.wind_kph
});
air.save((false) => {
if (false) {
console.error(error);
}
});
res.json(data);
} else {
console.error(error);
res.sendStatus(500);
}
});
});
`
- Start the server by adding the following code to the end of the "server.js" file:
`
const port = 3000;
app.listen(port,() => {
console.log("Port {server running on port");
});
`
Step 3: Create a frontend application with Angular
- Install Angular CLI globally: `npm install -g @angular/client'
- Create a new angular application by running "fresh-app- frontend".
- Change to the project directory: "cd weather-app-frontend"
- Create a new component to display the weather: "create weather component"
- Open the generated component file Weather.component.ts and update the code like this:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-weather',
templateUrl: './weather.component.html',
styleUrls: ['./weather.component.css']
})
export class WeatherComponent implements OnInit {
weatherData: any;
constructor(private http: HttpClient) { }
ngOnInit(): void {
this.getWeatherData('London');
}
getWeatherData(city: string): void {
this.http.get(`/api/weather/${city}`).subscribe(data => {
this.weatherData = data;
});
}
}
- *Open the created component template file *
"weather.component.html" and update the code:
<div *ngIf="airData">
<h2> {{weatherData.location.name}} </h2>
<p>Temperature: {{weatherData.current.temp_c}} °C</p>
<p>Humidity: {{weatherData.current.humidity}}%</p>
<p>Wind speed: {{weatherData.current.wind_kph}} kph </p>
</div>
- Update the "app.component.html" file to include the Air component:
<app-weather> </app-weather>
- Start the angular development server using:
"ng service" command
The results:
Congratulations! You have successfully built a weather application using the MEAN stack. This tutorial covers the steps of how to build a back-end server using Express.js and MongoDB, as well as creating a front-end application with Angular. By following this guide, newbies can gain valuable insight into how to integrate the MEAN stack technology to create a weather compatible application.