javascript - NodeJS Express 共享托管 GET 请求 index.php
问题描述
我有一个共享主机包。我创建了一个在本地运行的 NodeJS 应用程序,并试图让它在托管的网站上运行。但是,该网站返回 404 错误,因为它对 index.php 执行 GET 请求,而这显然不存在。实际的错误文本是:“Cannot GET /index.php” 下面是用于启动节点应用程序的 app.js 的代码。
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var mysql = require('mysql');
var session = require('express-session');
var indexRouter = require('./routes/index');
var dashboardRouter = require('./routes/dashboard');
var app = express();
global.connection = mysql.createConnection({
//deleted for security
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(session({resave: false, secret: 'this-is-a-secret-token', cookie: { maxAge: 600000 }}));
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/dash', dashboardRouter);
// catch 404 and forward to error handler
//app.use(function(req, res, next) {
// next(createError(404));
//});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
然后,我发现的唯一提议的解决方案都涉及修改 .htaccess 文件。下面是我的 .htaccess 看起来像 atm:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
这包括我尝试整合解决方案,但到目前为止还没有运气。我在这里做错了什么?
解决方案
一个可能会让你思考的小技巧:
app.get('/index.php',()=>{res.send("virtual index.php")})
或者...
app.get('/index.php',()=>{res.sendFile("/path/to/index.php")})
推荐阅读
- javascript - VueJS 2 - 无法在 App.vue 文件中使用导入文件中的变量
- php - 多行文本框到多记录插入MYSQL PHP
- sql - 2表join,ids IN list(),限制list中每个元素的model个数
- java - 从 EC2 访问 SQS - 实例配置文件与角色
- docker - 使用 localhost 进行 Docker 端口转发
- apache - 用于身份验证的 Apache 客户端证书
- java - iText7-Community AcroFields 被拉平
- c# - OleDbCommand ExecuteNonQuery 正在更改数据库的文件路径 (Access .accdb)
- c# - 使用 Entity Framework Core 2.x 在运行时更新数据库
- r - R中没有特定子矩阵的矩阵