【发布时间】:2021-03-17 14:31:51
【问题描述】:
我正在尝试使用 netlify 及其 lambda 函数功能来运行节点函数。基于 https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/ ,我在我的函数/submission-created.js 中有:
const https = require("https");
exports.handler = async event => {
const email = JSON.parse(event.body).payload.data.EMAIL
const asking = JSON.parse(event.body).payload.data.ASKING
var formData = {
'email': email,
'first_name': '',
'last_name': asking,
'lists[]': 'NUM'
};
var encoded = Object.entries(formData).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
var endpoint = 'https://api.sendfox.com/contacts/?' + encoded;
const data = JSON.stringify(formData);
const options = {
method: 'POST',
connection: 'keep-alive',
headers: {
'Authorization': 'Bearer hhhhh',
'Content-Type': 'application/json',
},
'content-length': data.length,
};
console.log(email);
const req = https.request(endpoint, options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
console.log(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(data);
req.end();
return {
statusCode: 200,
body: data
};
}
当我使用 netlify dev 在本地运行它时,这可以按预期工作,但是当推送到 netlify 用于构建站点的 github 存储库时,它在生产中不起作用。我该如何解决这个问题?
包结构如截图所示:
编辑:
netlify.toml:
[build]
functions = "./functions
没有错误。站点功能选项卡上的输出是:
8:57:43 PM: 2020-12-08T01:57:43.384Z undefined INFO to here
8:57:43 PM: 2020-12-08T01:57:43.390Z 8ca26edc-1f20-4c20-b038-
79ecdf206d92 INFO yt@ghj.org
8:57:43 PM: 2020-12-08T01:57:43.390Z 8ca26edc-1f20-4c20-b038-
79ecdf206d92 INFO 999
8:57:43 PM: 2020-12-08T01:57:43.390Z 8ca26edc-1f20-4c20-b038-
79ecdf206d92 INFO yt@ghj.org
8:57:43 PM: Duration: 39.71 ms Memory Usage: 69 MB Init Duration:
176.22 ms
【问题讨论】:
-
你的
netlify.toml配置文件的设置方式和文章中的一样吗?该功能是否运行不正常,或者根本没有显示在仪表板中? -
你已经详细说明了这个
it does not work in production.。你有什么错误吗? -
请看编辑
-
编辑不够。你期望什么与你得到什么相反?
-
你能分享你的文件夹结构和你在发布时使用的内容
标签: javascript node.js aws-lambda netlify