How to make a next js 14 app router api public?
Unanswered
Great golden digger wasp posted this in #help-forum
Great golden digger waspOP
Hey guys how can we make a next js 14 api public so we can access it from any domain. Using App router.
Please help me
This is my api.
Please help me
import products from "../../../utils/Schema";
import Users from "../../../utils/users";
import Contact from "../../../utils/Contact us";
import ConnectDB from "@/utils/connect";
const bcrypt = require("bcrypt");
import corsMiddleware from "../../../utils/cors";
import NextCors from "nextjs-cors";
require("dotenv").config();
export const GET = async (req, res) => {
ConnectDB(process.env.MONGO_URI);
try {
let data = await products.find();
return Response.json({ data });
} catch (error) {
console.log(error);
return Response.json({ "Error with getAllItems:": error });
}
};This is my api.
18 Replies
@Great golden digger wasp Hey guys how can we make a next js 14 api public so we can access it from any domain. Using App router.
Please help me
javascript
import products from "../../../utils/Schema";
import Users from "../../../utils/users";
import Contact from "../../../utils/Contact us";
import ConnectDB from "@/utils/connect";
const bcrypt = require("bcrypt");
import corsMiddleware from "../../../utils/cors";
import NextCors from "nextjs-cors";
require("dotenv").config();
export const GET = async (req, res) => {
ConnectDB(process.env.MONGO_URI);
try {
let data = await products.find();
return Response.json({ data });
} catch (error) {
console.log(error);
return Response.json({ "Error with getAllItems:": error });
}
};
This is my api.
send the cors http headers in the response. you can do it in [next.config.js](https://nextjs.org/docs/app/api-reference/next-config-js/headers#cors), [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware#cors) or [route handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers#cors)
Sun bear
I think its public by default
not really. cors protection by browsers means other frontends cannot fetch it, unless the api routes themselves agree to (by sending the relevant cors headers)
@Sun bear I think its public by default
Great golden digger waspOP
then create your first api using next js
@joulev not really. cors protection by browsers means other frontends cannot fetch it, unless the api routes themselves agree to (by sending the relevant cors headers)
Great golden digger waspOP
please make the important customization in my api so that it become public.
@Great golden digger wasp then create your first api using next js
Sun bear
😅 when I create /api/health/route.ts and I return {status: "okay"} i can access that route from everywhere. Everyone can visit the page and see it.
Or do you mean something else?
Or do you mean something else?
@Sun bear 😅 when I create /api/health/route.ts and I return {status: "okay"} i can access that route from everywhere. Everyone can visit the page and see it.
Or do you mean something else?
Great golden digger waspOP
bro I want to fetch it from different projects.
did you know what fetch api is?
Sun bear
Yes you can fetch(...api/health) and will get status okay on return.
But anyway. Hope you find a solution
But anyway. Hope you find a solution
@Sun bear Yes you can fetch(...api/health) and will get status okay on return.
But anyway. Hope you find a solution
Great golden digger waspOP
I need to fetch it from a different project using fetch api and this is causing cors issues and I don't know how to solve it. You better learn first.
you are being very rude. please stop. if you want to clarify that they were wrong, do so with normal mutual respect.
@Great golden digger wasp I need to fetch it from a different project using fetch api and this is causing cors issues and I don't know how to solve it. You better learn first.
Sun bear
You dont have to feel offended. I am here to help for free with my time because I like helping.
When you are not happy with my reply its okay but dont get your point in being childish
When you are not happy with my reply its okay but dont get your point in being childish
@joulev you are being very rude. please stop. if you want to clarify that they were wrong, do so with normal mutual respect.
Great golden digger waspOP
and please solve my problem.
I need to get the work done in one hour
Netherland Dwarf
Please remember everyone here is volunteer
No one is here to do free work for you. If you need help then please ask and also be friendly otherwise no one is going to help you with that attitude. Also if this is trolling, you could also be banned if this continues in every request as trolling is not tolerated in this server.
Kindly do not repost the question. You may bump this question once a day to draw attention. Reposting the question will create duplicates and is not allowed.
I will delete the duplicate.
I will delete the duplicate.