Which is better
import { NextResponse } from "next/server"; import Cors from 'cors'; const cors = Cors({ methods: ['GET'], origin: '*' }); let i = async (url) => { try { const response = await fetch(url || 'https://example.com'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.text(); return data; } catch (error) { console.error('Error:', error); throw error; } } function getParameterByName(name, url = window.location.href) { const urlObj = new URL(url); return urlObj.searchParams.get(name); } export async function GET(req, res) { try { await cors(req, res); const { url: rawUrl } = req; const url = getParameterByName("url", rawUrl); console.log(rawUrl); const result = await i(url); return new NextResponse(result, { status: 200 }); } catch (error) { console.error('Error:', error); return new NextResponse('An error occurred while processing the request.', { status: 500 }); } }
import { NextResponse } from "next/server"; import Cors from 'cors'; const cors = Cors({ methods: ['GET'], }); let i = async (url) => { let data = await fetch(url || 'https://example.com'); data = await data.text(); return data; } function getParameterByName(name, url = window.location.href) { name = name.replace(/\[\\\[\\\]\]/g, '\\\\$&'); var regex = new RegExp('\[?&\]' + name + '(=(\[^&#\]\*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\\+/g, ' ')); } export async function GET(req, res) { await cors(req, res); const { url: rawUrl } = req; const urlSearchParams = new URLSearchParams(rawUrl.search); const url = getParameterByName("url", rawUrl); console.log(rawUrl); const result = await i(url); return new NextResponse(`${result}`); }
CORS Error Fixed Here
import { NextResponse } from "next/server"; let i = async (url) => { let data = await fetch(url || 'https://example.com'); data = await data.text(); return data; } function getParameterByName(name, url = window.location.href) { name = name.replace(/\[\\\[\\\]\]/g, '\\\\$&'); var regex = new RegExp('\[?&\]' + name + '(=(\[^&#\]\*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\\+/g, ' ')); } export async function GET(req, res) { const { url: rawUrl } = req; const url = getParameterByName("url", rawUrl); console.log(rawUrl); const result = await i(url); return new NextResponse(result, { status: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', } }); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Which is better
CORS Error Fixed Here