简体   繁体   English

快速根路由在生产中不起作用

[英]Express root route not working in production

I created a simple Node application with 5 routes that serve different HTML documents. 我创建了一个简单的Node应用程序,该应用程序具有5条提供不同HTML文档的路由。 When running on localhost all routes are working, but when I run the app in production my root route is serving a 404 page, which means the route doesn't exist. 在本地主机上运行时,所有路由都可以使用,但是当我在生产环境中运行应用程序时,我的根路由将提供404页面,这意味着该路由不存在。

I have set up A2 hosting as instructed in their guide . 我已经按照他们的指南中的指示设置了A2主机。 You can test the app here . 您可以在此处测试该应用程序

I guess the problem is .htaccess file which I have copied from the guide, but I don't understand a single line in there. 我猜问题是从指南中复制的.htaccess文件,但我听不懂其中的任何一行。 Here is the code, located in public_html folder: 这是位于public_html文件夹中的代码:

app.js app.js

const path = require("path");

const express = require("express");
const bodyParser = require("body-parser");

const appRoutes = require("./routes/app");
const errorController = require("./controllers/error");

const app = express();
app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));

app.use(appRoutes);
app.use(errorController.get404);

app.listen(40000, () => console.log("Server is up on port 40000!"));

routes/app.js 路线/ app.js

const express = require("express");

const appController = require("../controllers/app");

const router = express.Router();

router.get("/", appController.getIndex);
router.get("/work", appController.getWork);
router.get("/services", appController.getServices);
router.get("/blog", appController.getBlog);
router.get("/contact", appController.getContact);

module.exports = router;

controllers/app.js 控制器/ app.js

exports.getIndex = (req, res, next) => {
  res.render("index");
};

exports.getWork = (req, res, next) => {
  res.render("work");
};

exports.getServices = (req, res, next) => {
  res.render("services");
};

exports.getBlog = (req, res, next) => {
  res.render("blog");
};

exports.getContact = (req, res, next) => {
  res.render("contact");
};

controllers/error.js 控制器/ error.js

exports.get404 = (req, res, next) => {
  res.status(404).render("404");
};

.htaccess 的.htaccess

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:40000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:40000/$1 [P,L]

Since you can reach your home page by port 40000 , the problem is definitly in the .htaccess file. 由于您可以通过端口40000进入主页 ,因此问题肯定在.htaccess文件中。

Can you try to add a slash to te last rule ? 您可以尝试在最后一条规则中添加斜杠吗?

RewriteRule ^/(.*)$ http://127.0.0.1:40000/$1 [P,L]

or add this before 或在此之前添加

RewriteBase /

The .htaccess file was the problem, it works now after adding this at the top: .htaccess文件是问题所在,在顶部添加以下文件后,它现在可以工作:

DirectoryIndex disabled

Thanks for helping! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM