NodeJS Express共享托管对index.php的GET请求

时间:2018-05-05 22:24:25

标签: javascript php node.js .htaccess express

我有一个共享主机包。我创建了一个在本地工作的NodeJS应用程序,并试图让它在托管的网站上运行。但是,该网站返回404错误,因为它执行index.php的GET请求,这显然不存在。实际的错误文本是:"不能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>

这包括我尝试整合解决方案,但到目前为止还没有运气。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

一个可能让你思考的小黑客:

app.get('/index.php',()=>{res.send("virtual index.php")})

...或

app.get('/index.php',()=>{res.sendFile("/path/to/index.php")})