我有一个带有Node.js服务器的React Web应用程序,我的网站的.htaccess
文件用于将网页请求重定向到index.html
,但是这意味着从网站上的表单发送的电子邮件没有被传递,因为请求必须被重定向到节点环境。通过将请求重定向到节点环境而不是.htaccess
文件中的index.html
,修复了此问题。这意味着我的服务器端路由现在中断了,每当我在浏览器窗口中键入带有端点的URL或刷新页面时,我现在都会收到Cannot GET /end-point
错误。
旧的.htaccess:
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
当前.htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
有没有一个修复既不中断我的服务器端路由或电子邮件传递?
编辑:
server.js:
const express = require("express");
const app = express();
require("dotenv").config({path: "../.env"});
const bodyParser = require("body-parser");
const cors = require("cors");
const nodemailer = require("nodemailer");
var sgTransport = require('nodemailer-sendgrid-transport');
const multer = require("multer");
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(cors());
const transport = nodemailer.createTransport(sgTransport({
auth: {
api_key: process.env.SG_API_KEY
}
}));
app.post("/send_schools_form", cors(), async (req, res) => {
let {
school,
address,
name,
phone,
email,
substitutes
} = req.body;
var substitutesList = substitutes.reduce(function(a, b) {
return a + '</br><ul><li>Startdatum: ' + b.date + '</li><li>Tid: ' + b.time + '</li> <li>Info: ' + b.info + '</li></ul>';
}, '');
await transport.sendMail({
from: process.env.USER_SENDER,
to: process.env.USER_RECEIVER,
subject: `Vikarie begäran ${school}`,
html: `
<ul>
<li>Skola: ${school}</li>
<li>Adress: ${address}</li>
<li>Kontaktperson: ${name}</li>
<li>Telefon: ${phone}</li>
<li>E-post: ${email}</li>
</ul>
<ul>${substitutesList}</ul>
`
}, (err) => {
if (err) {
console.log(err);
res.status(400).send('Error');
} else {
res.status(200).send('Success');
}
})
});
const upload = multer({
storage: multer.memoryStorage()
});
let middleware = [
cors(),
upload.fields([
{name: "cvFile", maxCount: 1},
{name: "otherFile", maxCount: 1}
])
];
app.post("/send_substitutes_form", middleware, async (req, res) => {
let {
firstName,
lastName,
email,
phone,
address,
city,
postalCode,
availability
} = req.body;
await transport.sendMail({
from: process.env.USER_SENDER,
to: process.env.USER_RECEIVER,
subject: `Arbetsansökan ${firstName} ${lastName}`,
html: `
<ul>
<li>Namn: ${firstName} ${lastName}</li>
<li>E-post: ${email}</li>
<li>Telefon: ${phone}</li>
<li>Adress: ${address}</li>
<li>Ort: ${city}, ${postalCode}</li>
<li>Tillgänglighet: ${availability} dagar per vecka</li>
</ul>
`,
attachments: [{
filename: req.files["cvFile"][0].originalname,
content: req.files["cvFile"][0].buffer
},
{
filename: req.files["otherFile"][0].originalname,
content: req.files["otherFile"][0].buffer
}]
}, (err) => {
if (err) {
console.log(err);
res.status(400).send('Error');
} else {
res.status(200).send('Success');
}
})
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log("Server is listening on port " + PORT);
});
1条答案
按热度按时间rks48beu1#
你做错了,你不应该用301重定向。
我假设你发送一封电子邮件通过张贴一个http请求到你的后端,所以你需要设置你所有的后端与特定的路径,如
/api
.然后你可以使用以下答案来解决你的问题:检查this和this