我想通过app.js(索引文件)在routes文件夹中运行index.js文件。
app.js中的以下代码在本地运行良好,但是当我将HTTP强制为https时,在生产中出现错误。
我的错误:
Cannot GET /index.php
我的app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var logger = require('morgan');
var cfenv = require('cfenv');
var uuid = require("uuid4");
var lti = require("ims-lti");
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
app.enable('trust proxy');
const sessions = {};
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.post("*", require("body-parser").urlencoded({extended: true}));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.post("/get", (req, res) => {
const moodleData = new lti.Provider("top", "secret");
moodleData.valid_request(req, (err, isValid) => {
const sessionID = uuid();
sessions[sessionID] = moodleData;
res.send(moodleData.body);
});
});
const port = 3000;
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');
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
module.exports = app;
我的.htaccess文件
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)?$ http://127.0.0.1:3000/$1 [P,L]
我不确定.htaccess文件中的错误吗?
答案 0 :(得分:0)
您是否使用反向代理重定向到Nodejs服务器?
查看本文
基本上,您需要创建一个虚拟主机并告诉Apache
<VirtualHost *:80>
...
ServerName mySite
ServerAlias www.abcd.com
...
...
...
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location />
ProxyPass http://127.0.0.1:3000
ProxyPassReverse http://127.0.0.1:3000
</Location>
</VirtualHost>
现在可以通过以下方式访问您的网站 www.abcd.com
启用HTTPS,并且您使用的是Linux尝试