i’m having some problems with Node.js Routes on CloudWays application,
I have this example code, and only the root route works ‘/’, the others got a status of 404, how this can be possible, i’m not an expert at Node.js i’m a front-end dev:
import express from "express";
import cors from "cors";
import fetch from "node-fetch";
const app = express();
app.use(cors()); // Habilitar CORS
// Defina seu token de acesso e store ID diretamente no código
const PORT = process.env.PORT || 3000;
app.get('/teste', (req, res) => {
res.send('<h1>Hello, your Node.js app is running on Cloudways!</h1>');
});
app.get("/produtos", async (req, res) => {
try {
const response = await fetch(
`https://api.tiendanube.com/v1/${storeId}/products`,
{
method: "GET",
headers: {
Authentication: `bearer ${accessToken}`,
"Content-Type": "application/json",
"User-Agent": "Loja ([email protected])",
},
}
);
if (!response.ok) {
throw new Error(`Erro: ${response.statusText}`);
}
const data = await response.json();
console.log(data);
res.json(data);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
const draftOrderData = {
contact_name: "João",
contact_lastname: "Paulo",
contact_email: "[email protected]",
payment_status: "unpaid",
products: [
{
variant_id: 1048977915, // Substitua pelo ID do produto variante
quantity: 1, // Propriedades customizadas, se necessário
},
],
};
app.get("/carrinho", async (req, res) => {
try {
const response = await fetch(
`https://api.tiendanube.com/v1/${storeId}/draft_orders`,
{
method: "POST",
headers: {
Authentication: `bearer ${accessToken}`,
"Content-Type": "application/json",
"User-Agent": "Loja ([email protected])",
},
body: JSON.stringify(draftOrderData),
}
);
if (!response.ok) {
throw new Error(`Erro: ${response.statusText}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
res.status(500).json({ message: error.message });
} finally {
return;
}
});
app.listen(PORT, () => {
console.log(`Servidor rodando na porta ${PORT}`);
});
just need that all routes work at the same file, so I dont need to create one code of Node for each route I have just to put it at the root route