我创建了一个简单的Node应用程序,该应用程序具有5条提供不同HTML文档的路由。在本地主机上运行时,所有路由都可以使用,但是当我在生产环境中运行该应用程序时,我的根路由正在提供404页面,这意味着该路由不存在。
我已经按照their guide中的说明设置了A2托管。您可以test the app here。
我想问题是我从指南中复制了.htaccess文件,但我看不懂其中的任何一行。这是位于public_html文件夹中的代码:
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
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
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
exports.get404 = (req, res, next) => {
res.status(404).render("404");
};
.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]
答案 0 :(得分:0)
由于您可以访问your home page by port 40000,所以问题肯定在.htaccess文件中。
您可以尝试在最后一条规则中添加斜杠吗?
RewriteRule ^/(.*)$ http://127.0.0.1:40000/$1 [P,L]
或在此之前添加
RewriteBase /
答案 1 :(得分:0)
.htaccess
文件是问题所在,在顶部添加此文件后即可正常工作:
DirectoryIndex disabled
感谢您的帮助!