使用 netlify.toml 时,Netlify 上的部署一直失败
Posted
技术标签:
【中文标题】使用 netlify.toml 时,Netlify 上的部署一直失败【英文标题】:Deployment keeps failing on Netlify when using netlify.toml 【发布时间】:2022-01-23 06:36:57 【问题描述】:我正在使用 React 开发 SPA,我正在使用带有 Express 和 Google API 的 Nodemailer 通过 /contactos 的联系表发送电子邮件。一切正常,但部署过程失败。
我有一个如下所示的netlify.toml
文件:
[build]
functions = "functions"
command = "node functions/mailer.js && npm run build"
一个名为 functions
的文件夹,其中我有一个名为 mailer.js
的文件,并且那里有与 Nodemailer 相关的所有代码:
require('dotenv').config();
const nodemailer = require('nodemailer');
const google = require('googleapis');
const express = require('express');
const path = require('path');
const app = express();
const cors = require('cors');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.urlencoded( extended: false ));
app.use(cors());
app.post('/enviar', (req, res) =>
console.log(req.body.formData);
const userInfo = req.body.formData;
const EMAIL = process.env.REACT_APP_EMAIL;
const CLIENT_ID = process.env.REACT_APP_CLIENT_ID;
const CLIENT_SECRET = process.env.REACT_APP_CLIENT_SECRET;
const REDIRECT_URI = process.env.REACT_APP_REDIRECT_URI;
const REFRESH_TOKEN = process.env.REACT_APP_REFRESH_TOKEN;
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oAuth2Client.setCredentials( refresh_token: REFRESH_TOKEN );
const sendMail = async () =>
try
const accessToken = await oAuth2Client.getAccessToken();
const transporter = nodemailer.createTransport(
service: 'gmail',
auth:
type: 'OAuth2',
user: EMAIL,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken,
,
);
let mailOptions =
from: `$userInfo.firstName $userInfo.lastName <$userInfo.email>`,
to: EMAIL,
subject: `Correo enviado por $userInfo.firstName $userInfo.lastName desde el sitio web`,
text: `$userInfo.message`,
html: `
<p>¡Tienes un nuevo correo electrónico enviado desde el sitio web de LynStore!</p>
<h3>Detalles del contacto:</h3>
<ul>
<li>Nombre: $userInfo.firstName $userInfo.lastName</li>
<li>Correo: $userInfo.email</li>
</ul>
<h3>Mensaje:</h3>
<p>$userInfo.message</p>
`,
;
const result = await transporter.sendMail(mailOptions);
return result;
catch (err)
console.log('Error!!!!!!!', err);
;
sendMail()
.then((result) => res.json( status: 'success' ))
.catch((error) =>
res.json( status: 'fail' );
console.log('Error en el envío!', error.message);
);
);
const PORT = process.env.PORT || 3001;
app.listen(PORT, () =>
console.log(`Server is running on port: $PORT`);
);
这些是 Netlify 上部署日志的最后 27 行:
4:09:12 PM: ────────────────────────────────────────────────────────────────
4:09:12 PM: Netlify Build
4:09:12 PM: ────────────────────────────────────────────────────────────────
4:09:12 PM:
4:09:12 PM: ❯ Version
4:09:12 PM: @netlify/build 26.0.2
4:09:12 PM:
4:09:12 PM: ❯ Flags
4:09:12 PM: baseRelDir: true
4:09:12 PM: buildId: 61c2425eebc157000771ab02
4:09:12 PM: deployId: 61c2425eebc157000771ab04
4:09:12 PM:
4:09:12 PM: ❯ Current directory
4:09:12 PM: /opt/build/repo
4:09:12 PM:
4:09:12 PM: ❯ Config file
4:09:12 PM: /opt/build/repo/netlify.toml
4:09:12 PM:
4:09:12 PM: ❯ Context
4:09:12 PM: production
4:09:12 PM:
4:09:12 PM: ────────────────────────────────────────────────────────────────
4:09:12 PM: 1. build.command from netlify.toml
4:09:12 PM: ────────────────────────────────────────────────────────────────
4:09:12 PM:
4:09:12 PM: $ node functions/mailer.js && npm run build
4:09:13 PM: Server is running on port: 3001
4:38:47 PM: Build exceeded maximum allowed runtime
【问题讨论】:
【参考方案1】:Nelify 函数不像标准服务器那样运行,它们的运行时间限制为 15 分钟,它们是无服务器的,并且您的代码应该以支持这一点的方式编写,而不是被视为标准服务器部署。
这里提供了一个很好的例子:
https://github.com/neverendingqs/netlify-express/blob/0780127cd575704e2a2a00a1a648ba5a5a66c388/express/server.js
【讨论】:
以上是关于使用 netlify.toml 时,Netlify 上的部署一直失败的主要内容,如果未能解决你的问题,请参考以下文章
Netlify 在页面刷新时呈现 404(使用 React 和 react-router)
在 Netlify 上部署 gatsby 站点时响应错误的大小图像
为啥我的 CSS 在使用 xampp 的 localhost 中不能正常工作,但在本地或从 Netlify 站点打开相同的文件时工作正常?