【问题标题】:NodeJS Express shared hosting GET request for index.phpNodeJS Express 共享主机 GET 请求 index.php
【发布时间】:2018-05-06 08:55:09
【问题描述】:

我有一个共享主机包。我创建了一个在本地运行的 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>

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

【问题讨论】:

  • 您确定您的共享主机允许使用 node.js 服务器吗?
  • 另外,你为什么要在 node.js 应用程序中加载 index.php?您是否正在尝试运行 PHP 代码?

标签: javascript php node.js .htaccess express


【解决方案1】:

一个可能让你思考的小技巧:

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

或者...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多