【问题标题】:Improper Routing when deploying Node.js app to Render VPS将 Node.js 应用程序部署到 Render VPS 时路由不正确
【发布时间】:2020-07-25 10:14:05
【问题描述】:

我正在通过 Render 将 Node.js Express 应用程序部署到 VPS。当我在本地机器上运行应用程序时,当我将浏览器指向 localhost:3001 时,npm start 命令可以很好地提供我的文件。但是,在我部署后,基目录“/”返回“未找到”。我必须将我的浏览器指向 example.onrender.com/public/index.html。

如何确保 example.onrender.com/ 将请求路由到 public/index.html?

谢谢!

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res) {
  res.render('index.html');
});


app.listen(3001);

【问题讨论】:

    标签: node.js express url routes


    【解决方案1】:

    实际上只需将渲染中的“发布目录”设置更改为 ./public

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2012-06-17
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      相关资源
      最近更新 更多