我的應用程式在本地主機上運行得非常好,但是一旦我部署到 Heroku,我就遇到了這個錯誤:
當我在客戶端使用 console.log(response.data) 時,我收到了這個字串,而不是帶有我的用戶資訊的 res.json:
"<!doctype html><html lang="en"><meta charset="utf-8"/><link rel="icon" href="/climbing.png"/><meta name="viewport" 內容="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="使用 create-react 創建的網站-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/>攀登者國度<script defer=" defer" src="/static/js/main.56943839.js"><link href="/static/css/main.a3875cba.css" rel="stylesheet">您需要啟用 JavaScript 才能運行此應用。< div id="根">"
app.use(
cors({
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
credentials: true,
origin: ["https://climber-nation.herokuapp.com/"],
methods: ["GET", "POST"],
})
);
//VALIDATE AUTHENTICATION TOKEN
const authenticateToken = (req, res, next) => {
try {
const token = req.headers["authorization"];
if (token == null) {
return res.sendStatus(401);
} else {
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (error, decoded) => {
if (error) {
return res.sendStatus(403);
} else {
req.user = decoded;
return next();
}
});
}
} catch (error) {
console.log(error);
return res.status(400).json({ error: error });
}
};
//AUTHENTICATION
app.get("/authentication", authenticateToken, async (req, res) => {
const username = req.user.username;
const allUserInfo = await db("users").select("*").where("username", username);
const image = await db("images")
.innerJoin("users", "images.image_id", "users.user_id")
.select("filename")
.where("username", username);
const imageFile =
"https://climber-nation.herokuapp.com/images/" image[0]?.filename;
res.json({
allUserInfo: allUserInfo[0],
imageFile: imageFile,
});
});
uj5u.com熱心網友回復:
有朋友幫我解決。
我的服務器中有這些代碼行,但有一個小錯字,還需要重新排列它們所在的位置。
服務器應用程式的開始:
app.use(express.static(path.join(__dirname, "build")));
服務器應用程式結束:
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
我部署的應用程式現在可以正常作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440872.html
標籤:javascript 节点.js 反应 表示
上一篇:Express路由器在本地作業正常,但在Heroku上卻不行
下一篇:React中無法識別關鍵道具
