How to phrase fetch URL in PUT request for production with process.cwd #15271
-
Hello everyone! I dont know how to exactly phrase the fetch URL in a PUT request to API endpoint that updates a SQLite Database. In development I had this: const updateColumn = async (id, column) => {
const endpoint = `http://localhost:3000/api/posts/${id}`;
try {
await fetch(endpoint, {
method: "PUT",
headers: {
"Content-type": "application/json; charset=UTF-8",
},
body: JSON.stringify({ column: column }),
});
} catch (error) {
console.error(error);
}
}; I tried using I can get the json data manually here Big Thanks in Advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use NEXT_PUBLIC within an env file to use different API endpoints based upon the environment. By default, env variables are converted to strings, so they don't need quotes. env.development env.production const updateColumn = async (id, column) => {
try {
await fetch(`${process.env.NEXT_PUBLIC_API}/posts/${id}`, {
method: "PUT",
headers: {
"Content-type": "application/json; charset=UTF-8",
},
body: JSON.stringify({ column: column }),
});
} catch (error) {
console.error(error);
}
}; |
Beta Was this translation helpful? Give feedback.
You can use NEXT_PUBLIC within an env file to use different API endpoints based upon the environment.
By default, env variables are converted to strings, so they don't need quotes.
env.development
NEXT_PUBLIC_API=http://localhost:3000/api
env.production
NEXT_PUBLIC_API=https://nextjs-blog.christiankozalla.vercel.app/api