簡體   English   中英

快速路由在本地服務時有效,但一旦部署就會​​失敗

[英]Express routes work when serving locally but fail once deployed

我正在使用Node js和Express構建博客並將其托管在firebase上。 當我在本地提供網站服務時,一切正常,並且html可以按預期提供。 但是,當我部署服務器時,路由不再起作用,並且找不到html文件。 我確定這與Firebase部署如何保存html文件有關。

我不太確定該從哪里去。 我真的找不到如何在Firebase文檔上設置類似內容的指導。

const functions = require("firebase-functions")
const cors = require("cors")
const express = require("express")
const path = require("path")

/* Express with CORS */
const app = express()
app.use(cors({ origin: true }))
app.get("/", (request, response) => {
  response.send("Hello from Express on Firebase with CORS!")
})

//File path consts
const publicDir = "/Users/wilson/wildman-talks-fb/public";
const blogDir = "/Users/wilson/wildman-talks-fb/public/blogs";

app.get("/about/", (req, res) =>{
    res.sendFile(path.join(publicDir, "/about.html"));
});

app.get("/contact/", (req, res) =>{
    res.sendFile(path.join(publicDir, "/contact.html"));
});

app.get("/tools/", (req, res) =>{
    res.sendFile(path.join(publicDir, "/tools.html"));
});

app.get("/five-steps-july-20/", (req, res) =>{
    //res.send(path.join(publicDir, "/five-steps-july-20.html"));
    res.sendFile(path.join(publicDir, "/five-steps-july-20.html"));
})

exports.app = functions.https.onRequest(app)

所以發生了什么事,當我在本地部署網站時,我網頁中所有鏈接的工作都鏈接到該網站的其他html網頁。 當我在Firebase上部署它時,出現404錯誤。 我能夠使用path.join(__ dirname,“ ../public”)並打印出其中包含的所有文件。 當我這樣做時,這些是本地主機上的文件:[“ .DS_Store”,“ 404.html”,“ about.html”,“博客”,“ contact.html”,“ css”,“ 5 -steps-july-20.html“,” img“,” index.html“,” js“,”郵件“,” tools.html“,”供應商“]。 部署后,它僅返回500錯誤,因此我想這無濟於事。

目錄包含文件系統的絕對路徑。 嘗試使用動態絕對路徑。 更改路徑

  const publicDir = "/Users/wilson/wildman-talks-fb/public";
  const blogDir = "/Users/wilson/wildman-talks-fb/public/blogs";

  const path = require("path");
  const publicDir = path.join(__dirname, "/public";)
  const blogDir = path.join( __dirname, "/public/blogs");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM