DEV Community

Chabba Saad
Chabba Saad

Posted on

HTTPS problem with SSL certificates in Node.js

Bypassing the HTTPS problem with SSL certificates in Node.js involves temporarily disabling certificate validation or using self-signed certificates for development or testing purposes. Here's a short description of the steps you can take:

const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const nodemailer = require('nodemailer');
const fs = require('fs');
const https = require('https');

const app = express();
const port = 3000;

app.use(express.static(__dirname + '/var/www/html'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

const options = {
    key: fs.readFileSync('/etc/letsencrypt/live/domaine.com/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/domaine.com/cert.pem')
};
const corsOptions = {
    origin: ['https://domaine.com', 'https://www.domaine.com'],

};


app.use(cors(corsOptions));

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "https://domaine.com, https://www.domaine.com");
    next();
});

app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname + '/var/www/html/index.html'));
});

app.use(bodyParser.json());

// Your send-email route here
app.post('/send-email', (req, res) => {
    const name = req.body.name;
    const email = req.body.email;
    const message = req.body.message;

    const transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'useremail@gmail.com', // Replace with your email address
            pass: 'motpasse' // Replace with your app password
        }
    });

    const mailOptions = {
        from: 'useremail@gmail.com',
        to: 'toemail@gmail.com', // Replace with recipient email address
        subject: 'Portfolio Message',
        text: `
      name: ${name}
      email: ${email}
      message: ${message}
    `
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            console.log(error);
            res.status(500).send('Error sending email');
        } else {
            console.log('Email sent:', info.response);
            res.status(200).send('Email sent successfully');
        }
    });
});

const server = https.createServer(options, app).listen(port, () => {
    console.log(`Server listening at https://domaine.com:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)