I’m trying to use netlify and its lambda function feature to run a node function . Based on Using Netlify Forms and Netlify Functions to Build an Email Sign-Up Widget | CSS-Tricks - CSS-Tricks , I have in my functions/submission-created.js:
const https = require("https");
exports.handler = async event => {
const email = 'bill66666@mailinator.com';
const asking = '70000';
var formData = {
'email': email,
'first_name': '',
'last_name': asking,
'lists[]': '11111'
};
var encoded = await Object.entries(formData).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
var endpoint = 'https://api.sendfox.com/contacts/?' + encoded;
const options = {
method: 'POST',
headers: {
'Authorization': 'Bearer XXXXXXXXX ',
'Content-Type': 'application/json',
}
};
const req = await https.request(endpoint, options, (res) => {
await console.log('statusCode:', res.statusCode);
await console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
}
return;
I’ve tested the code without the
exports.handler = async event => {...} function
and it works when run as a node script at the command line.
The package structure looks like:
here is the repo :
https://github.com/kc1/test2
However when I submit the form on page offer.html, it runs till the async code block but does nor execute anything inside it. What am I doing wrong?