[英]NodeJS Express shared hosting GET request for 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")})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.