web_next/weather_app/app/api/weather/route.ts
2024-04-25 14:56:39 -05:00

19 lines
No EOL
648 B
TypeScript

import axios from "axios";
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest){
try {
const searchParams = req.nextUrl.searchParams;
const lat = searchParams.get("lat");
const lon = searchParams.get("lon");
const apiKey = process.env.OPENWEATHERMAP_API_KEY;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`
const res = await axios.get(url);
return NextResponse.json(res.data);
} catch (error){
console.log(error)
return new Response('Error', {status: 500});
}
}