5
Mar

nodejs server

   Posted by: admin   in Mẹo vặt của hiếu râu

mkdir mailform
mv mailform.js mailform
cd mailform/
yum install npm
npm init -y
npm install express express-rate-limit
node mailform.js
while true; do date; node mailform.js ; sleep 1; done

const express = require('express');
const rateLimit = require('express-rate-limit');
const bodyParser = require('body-parser');
const https = require('https');
const fs = require('fs');

const app = express();
const options = {
key: fs.readFileSync('private.key'),
cert: fs.readFileSync('public.crt')
}

// Apply rate limiting middleware
const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 2, // limit each IP to 2 requests per windowMs
message: 'Too many requests from this IP, please try again later.'
});
app.use(limiter);
app.use(bodyParser.json());

app.options('*',(req,res) => {
res.status(200).end();
} );

// Your webhook endpoint
app.get('/bm/:email', (req, res) => {
// Handle webhook logic here
const requestBody = req.params.email;
console.log('Received webhook request:', requestBody);

// Options for the POST request
const options = {
hostname: 'hostname.com', // Replace with your hostname
port: 443, // Replace with your port number

path: '/hooks/catch/y/x/' , // '/your/post/endpoint', // Replace with your endpoint path
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify({'email':requestBody}).length
}
};

const zap = https.request(options, (zres) => {
console.log(`statusCode: ${zres.statusCode}`);

zap.on('data', (chunk) => {
console.log('Zapier Response body:', chunk.toString());
});
});

// Send a custom response
res.status(200).json({ message: 'in : '+requestBody });

// Handle request errors
zap.on('error', (error) => {
console.error('Error sending zapier request:', error);
});

// Write data to request body
zap.write(JSON.stringify({'email':requestBody}));

// End the request
zap.end();

});
//--------END GET--------------

app.post('/contact', (req, res) => {
// Handle webhook logic here
let requestBody = ''; // Assuming JSON request body

req.on('data', chunk => {
requestBody += chunk;
});
req.on('end',() => {
console.log('Received contact request:', requestBody);

// Options for the POST request
const options = {
hostname: 'hostname.com', // Replace with your hostname
port: 443, // Replace with your port number

path: '/hooks/catch/a/b/' , // Replace with your endpoint path
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': requestBody.length
}
};

const zap = https.request(options, (zres) => {
console.log(`statusCode: ${zres.statusCode}`);

zap.on('data', (chunk) => {
console.log('Zapier Response body:', chunk.toString());
});
});

// Send a custom response
res.status(200).json({ message: 'zap contact : '+requestBody });

// Handle request errors
zap.on('error', (error) => {
console.error('Error sending zapier request:', error);
});

// Write data to request body
zap.write(requestBody);

// End the request
zap.end();

} );

});
//=======END POST=========

const server = https.createServer(options,app);

// Start the server
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

This entry was posted on Tuesday, March 5th, 2024 at 2:01 am and is filed under Mẹo vặt của hiếu râu. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed at this time.