繁体   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