A simple and robust backend service for fetching weather data and managing a user's favorite locations.
This project is a backend API built with Node.js, Express, and TypeScript. It provides endpoints for user authentication, fetching real-time weather data from an external source, and allowing authenticated users to save and manage a list of their favorite locations.
You can test the live, deployed API at the following base URL.
Base URL: https://weather-api-s2e8.onrender.com/
Follow these instructions to get a local copy up and running for development and testing.
Make sure you have the following installed on your machine:
-
Clone the repository
git clone <YOUR_REPOSITORY_URL> cd <YOUR_PROJECT_DIRECTORY>
-
Install NPM packages
npm install
-
Set up Environment Variables Copy the example environment file. Then, open the new
.envfile and add your secret keys (e.g., your own weather API key).cp .env.example .env
Your
.env.devfile should look like this:PORT=3000 API_WEATHER_KEY=your_secret_weather_api_key JWT_SECRET=your_jwt_secret_key NODE_ENV=development ALLOWED_ORIGINS=http://localhost:5173
Your
.env.prodfile should look like this:PORT=3000 API_WEATHER_KEY=your_secret_weather_api_key JWT_SECRET=your_jwt_secret_key NODE_ENV=production ALLOWED_ORIGINS=https://weather-app-one-psi-89.vercel.app
-
Build the project This will compile the TypeScript code into JavaScript.
npm run build
-
Run the server
npm start
The server should now be running locally on
http://localhost:3000.
You can use an API client like Postman to test the following endpoints. Remember to prepend the base URL: https://weather-api-s2e8.onrender.com
Creates a new user account.
- URL:
https://weather-api-s2e8.onrender.com/signup - Request Body:
{ "userName": "someusername", "password": "somepassword" }
Logs in an existing user and returns a JWT.
- URL:
https://weather-api-s2e8.onrender.com/login - Request Body:
{ "userName": "someusername", "password": "somepassword" } - Response:
On success, you will receive a JSON object containing an
accessToken. Include this token in theAuthorizationheader for protected routes (e.g.,Authorization: Bearer <your_token>).
Logs out the user.
- URL:
https://weather-api-s2e8.onrender.com/logout
Fetches weather data for a specific location.
-
URL:
https://weather-api-s2e8.onrender.com/weather -
Query Parameters:
- Provide
cityORlatandlong. - Example:
.../weather?city=London - Example:
.../weather?lat=40.7128&long=-74.0060
- Provide
-
Example Response:
{ "city": "Delhi", "temperature": 34, "description": "Haze", "humidity": 45, "windSpeed": 5.1 // ... and other relevant weather data }
These routes require authentication. You must include the JWT in the Authorization header.
Saves a location's weather data to the user's favorites list.
- URL:
https://weather-api-s2e8.onrender.com/favourites/save - Request Body:
- Pass the full JSON object received from the
/weatherendpoint.
- Pass the full JSON object received from the
Retrieves the list of all favorite locations for the logged-in user.
- URL:
https://weather-api-s2e8.onrender.com/favourites
Removes a specific location from the user's favorites list.
- URL:
https://weather-api-s2e8.onrender.com/favourites/remove/:id - URL Parameter:
:id- The unique identifier of the favorite entry to be deleted.