我正在尝试让我的服务器使用node.js'*'路径为任意随机url参数提供index.html服务,但是仅当我访问没有url参数的站点时才使用它(示例.com)。如果我转到example.com/random,它将发送回“ cannot GET / random”
但是,当我在本地计算机上运行它时,它工作得很好。但是,当它在我的Nginx反向代理后面的服务器上运行时,它不起作用。
非常感谢您的帮助,这真的让我感到困惑。
以下是我的server.js和nginx默认配置文件的相关部分:
#HTTP - redirect all traffic to HTTPS
server {
listen 80;
listen [::]:80 default_server;
server_name northernreachnh.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
location / {
proxy_pass "http://localhost:8080/";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
const port = process.env.PORT || 8080;
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
app.get ('*', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get ('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// POST route from contact form
app.post('/contact', (req, res) => {
// Instantiate the SMTP server
const smtpTrans = nodemailer.createTransport({
host: 'xxxxxxx',
port: xxx,
auth: {
user: `xxxxx`,
pass: `xxxxx`
}
});
// Specify what the email will look like
const mailOpts = {
from: 'Your sender info here', // This is ignored by Gmail
to: `xxxxx`,
subject: 'New message from contact form',
text: `${req.body.name} (${req.body.tel}) says: ${req.body.email}`
};
// Attempt to send the email
smtpTrans.sendMail(mailOpts, (error, response) => {
if (error) {
res.sendFile(__dirname + '/error.html'); // Show a page indicating failure
}
else {
res.sendFile(__dirname + '/success.html'); // Show a page indicating success
}
});
});
app.listen(port, () => {
console.log("Server listening on Port 8080");
});