Cursor is revolutionizing how developers write code. As an AI-powered fork of VS Code, it offers features like intelligent code completion, AI-driven debugging, and seamless integration with tools like GPT-4. In this tutorial, you’ll build a full-stack weather application using Cursor’s AI capabilities to streamline development. By the end, you’ll understand how to leverage Cursor for planning, coding, testing, and deploying projects faster than ever.
What You’ll Build
- Frontend: A responsive UI with search functionality to fetch weather data.
- Backend: A Node.js/Express server to handle API requests.
- Features:
- Real-time weather data from OpenWeatherMap API.
- Error handling for invalid cities.
- Dynamic background based on weather conditions.
Step 1: Setting Up Cursor
Installation
- Download Cursor from cursor.sh.
- Install it like any standard desktop app (Windows, macOS, or Linux).
Initial Configuration
- Enable AI Features: Go to Settings > AI and enable “Auto-Suggest” and “Code Chat.”
- Install Extensions:
- Thunder Client: For testing API endpoints.
- GitLens: For version control.
Step 2: Planning with Cursor’s AI
Use Cursor’s /ask command to brainstorm project structure:
Cursor suggests:
- Separate
client
andserver
folders. - Use Axios for API calls.
- Environment variables for API keys.
Step 3: Creating the Project
Initialize the Backend
- In Cursor, open the terminal (
Ctrl + \
). - Create folders:
mkdir weather-app && cd weather-app mkdir server client
- Initialize the Node.js server:
cd server npm init -y npm install express axios dotenv cors
Set Up the Frontend
- Use Vite for the React frontend:
cd ../client npm create vite@latest . -- --template react npm install npm install axios
Step 4: Building the Backend with AI Assistance
Create server.js
- In Cursor, open
server/server.js
. - Use AI to generate boilerplate code:
/ai "Create an Express server that fetches weather data from OpenWeatherMap API"
Cursor generates:
const express = require('express'); const axios = require('axios'); require('dotenv').config(); const cors = require('cors'); const app = express(); app.use(cors()); const PORT = 5000; app.get('/weather/:city', async (req, res) => { try { const response = await axios.get( `https://api.openweathermap.org/data/2.5/weather?q=${req.params.city}&appid=${process.env.API_KEY}&units=metric` ); res.json(response.data); } catch (error) { res.status(404).json({ error: 'City not found' }); } }); app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
- Add your OpenWeatherMap API key to
.env
:API_KEY=your_api_key_here
Test with Thunder Client
- Open Thunder Client in Cursor.
- Send a GET request to
http://localhost:5000/weather/London
to verify the API.
Step 5: Developing the Frontend
Fetch Data from Backend
- In
client/src/App.jsx
, use AI to generate the fetch logic:/ai "Create a React component that searches for a city and displays weather data"
Cursor outputs:
import { useState } from 'react'; import axios from 'axios'; function App() { const [city, setCity] = useState(''); const [weather, setWeather] = useState(null); const [error, setError] = useState(''); const fetchWeather = async () => { try { const response = await axios.get(`http://localhost:5000/weather/${city}`); setWeather(response.data); setError(''); } catch (err) { setError('City not found'); setWeather(null); } }; return ( <div> <input type="text" value={city} onChange={(e) => setCity(e.target.value)} placeholder="Enter city" /> <button onClick={fetchWeather}>Search</button> {error && <p>{error}</p>} {weather && ( <div> <h2>{weather.name}</h2> <p>Temp: {weather.main.temp}°C</p> <p>{weather.weather[0].description}</p> </div> )} </div> ); } export default App;
Style with AI-Powered Suggestions
Ask Cursor:
Cursor recommends using conditional classes based on weather conditions (e.g., sunny, rainy).
Step 6: Debugging with Cursor
- AI Error Analysis: Highlight an error in your code and press
Ctrl+K
to let Cursor explain it. - Fix Bugs: For a failed API call, Cursor might suggest:
- Checking CORS settings.
- Verifying the API key in
.env
.
Step 7: Deploy the Project
- Deploy Backend to Render:
- Commit code to GitHub.
- Create a Render web service linked to your repo.
- Add environment variables in Render’s dashboard.
- Deploy Frontend to Netlify:
cd client npm run build drag-and-drop the `dist` folder to Netlify.
Cursor Pro Tips
- Refactor Code: Use
Ctrl+L
to highlight code and ask, “Can this be more efficient?” - Generate Tests:
/ai "Write a Jest test for the weather API endpoint"
. - Commit Messages: Let Cursor draft Git commit messages based on code changes.
Conclusion
In under an hour, you’ve built a full-stack app using Cursor’s AI to accelerate coding, debugging, and deployment. By integrating AI into your workflow, you reduce boilerplate tasks and focus on creativity.
Challenge Yourself: Enhance this app with hourly forecasts or user authentication—Cursor’s AI can guide you through those too!