diff --git a/package.json b/package.json index f8936e0..acfec99 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node src/server.js" + "start": "NODE_OPTIONS='--dns-result-order=ipv4first' node src/server.js" }, "author": "ryan", "license": "ISC", diff --git a/src/formHandler.js b/src/formHandler.js index 1481118..ea5c110 100644 --- a/src/formHandler.js +++ b/src/formHandler.js @@ -25,16 +25,31 @@ export async function submitToListmonk(req, res) { console.log('Sending request to Listmonk with payload:', payload); + async function fetchWithRetry(url, options, retries = 3) { + for (let i = 0; i < retries; i++) { + console.log(`Attempt ${i + 1} sending request to ${url}`); + try { + const res = await fetch(url, options); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + return res; + } catch (err) { + console.log(`Attempt ${i + 1} failed: ${err}`); + if (i === retries - 1) throw err; + await new Promise(resolve => setTimeout(resolve, 500)); + } + } + } + try { // Add subscriber - const response = await fetch(listmonkUrl, { + const response = await fetchWithRetry(listmonkUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${auth}` }, body: JSON.stringify(payload), - }); + },3); if (!response.ok) { const errorBody = await response.text(); @@ -64,13 +79,13 @@ export async function submitToListmonk(req, res) { }, body: JSON.stringify(statusPayload), }); - + if (!statusResponse.ok) { const errorBody = await statusResponse.text(); console.error('Response from Listmonk:', errorBody); throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`); } - + const statusResult = await statusResponse.json(); console.log('Campaign status updated successfully:', statusResult); } catch (error) { diff --git a/testDns.js b/testDns.js new file mode 100644 index 0000000..0da14b0 --- /dev/null +++ b/testDns.js @@ -0,0 +1,5 @@ +import dns from 'dns'; + +dns.lookup('mailer.melonion.me', { all: true }, (err, addresses) => { + console.log(addresses); +});