74
index.js
Normal file
74
index.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const helmet = require('helmet');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
const app = express();
|
||||
const ENV = process.env.ENVIRONMENT || 'TEST';
|
||||
const SENDER_EMAIL = process.env.EMAIL;
|
||||
const SENDER_PASS = process.env.EMAIL_PASSWORD;
|
||||
const SERV_HOST = process.env.EMAIL_HOST;
|
||||
const SERV_PORT = process.env.EMAIL_PORT;
|
||||
|
||||
const allowedOrigins = process.env.ALLOWED_ORIGINS.split(',');
|
||||
|
||||
app.disable('x-powered-by');
|
||||
app.use(express.json());
|
||||
app.use(helmet());
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
origin: function (origin, callback) {
|
||||
if (!origin || allowedOrigins.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(new Error(`Origin ${origin} is not allowed by CORS!`));
|
||||
}
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
// 2 requests per 5 minutes
|
||||
const limiter = rateLimit({
|
||||
windowMs: 5 * 60 * 1000,
|
||||
max: 2,
|
||||
});
|
||||
|
||||
app.use(limiter);
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: SERV_HOST,
|
||||
port: SERV_PORT,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: SENDER_EMAIL,
|
||||
pass: SENDER_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
app.post('/api/mail', (req, res) => {
|
||||
const { to, subject, text } = req.body;
|
||||
|
||||
let status = false;
|
||||
const mail = {
|
||||
from: `"Arbeit Mail Hizmeti" <${SENDER_EMAIL}>`,
|
||||
to,
|
||||
replyTo: 'noreply@arbeit.studio',
|
||||
subject,
|
||||
text,
|
||||
};
|
||||
|
||||
if (ENV === 'PROD') {
|
||||
if (transporter.sendMail(mail)) res.status(200).json({ message: 'Mail sent successfully!' });
|
||||
else res.status(500).json({ message: 'Mail could not be sent!' });
|
||||
} else res.status(200).json(mail);
|
||||
});
|
||||
|
||||
app.get('/api/hello', (req, res) => {
|
||||
res.status(200).json({ message: 'Hello, World!' });
|
||||
});
|
||||
|
||||
app.listen(3313, () => {
|
||||
console.log('Server up on 3313');
|
||||
});
|
||||
Reference in New Issue
Block a user