Copy const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
const { OneDrive } = require('onedrive-api');
const fetch = require('node-fetch');
const app = express();
app.use(bodyParser.json());
app.post('/donate', (req, res) => {
const { project, amount } = req.body;
// Integrate with CleanHub API for plastic cleanup donations
fetch('https://api.cleanhub.com/donate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_CLEANHUB_API_KEY'
},
body: JSON.stringify({ project, amount })
})
.then(response => response.json())
.then(data => {
console.log(`Donation received: $${amount} for ${project}`);
res.json({ message: 'Donation successful' });
})
.catch(error => {
console.error('Error:', error);
res.status(500).json({ message: 'Donation failed' });
});
});
app.post('/plant', (req, res) => {
const { location, treeType, email } = req.body;
// Integrate with Tree-Nation API for tree planting
fetch('https://api.tree-nation.com/plant', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_TREE_NATION_API_KEY'
},
body: JSON.stringify({ location, treeType })
})
.then(response => response.json())
.then(data => {
console.log(`Tree planted: ${treeType} at ${location}`);
res.json({ message: 'Tree planting successful' });
if (email) {
// Send planting certificate via email
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected] ',
pass: 'your-email-password'
}
});
const mailOptions = {
from: '[email protected] ',
to: email,
subject: 'Tree Planting Certificate',
text: `A ${treeType} tree has been planted at ${location}. Thank you for your contribution!`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Error sending email:', error);
} else {
console.log('Email sent:', info.response);
}
});
}
})
.catch(error => {
console.error('Error:', error);
res.status(500).json({ message: 'Tree planting failed' });
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});