Fastify to SQL Database (From a form)

138 views Asked by At

FASTIFY USED IN GLITCH.COM!

Basically, I am a rookie at backend. I have a server.js, which reads an index.ejs file. In this index.ejs file, i have a form which takes in as an input the user's email.

    <form id="myForm">
      
    <p>Subscribe now to get the latest updates!</p>
    <div class="input-box">
      <input id="email" name = "email" type="email" placeholder="Enter your email..." />
      <button type="submit" onclick="submitForm()">Notify Me</button>
     </div>
    </form>

When the user presses Notify Me, it should, (1) send the data into an SQL database (2) send the email using Nodemailer (Ive figured out the Nodemailer part, all i want is a temporary variable which remembers the email)

This is my server :

   const fastify = require('fastify')({ logger: true });

fastify.register(require('@fastify/view'), {
  engine: {
    ejs: require('ejs')
  },
  templates: 'views'
});


// Mock user data (in-memory storage for simplicity, replace with a database in a real application)
const users = [];

fastify.get('/', (req, reply) => {
  reply.view('index.ejs', { users });
});



//server.is
require( "dotenv") .config() ;
const nodemailer = require( "nodemailer");
//Step 1
let transporter = nodemailer.createTransport ({
service: "gmail",
auth: {
user: process. env. EMAIL,
pass: process.env.PASSWORD
}
});

let mailOptions = {
to: "",
subject: "Testing and Testing",
html: '<h2>Welcome, </h2><p>Your email is: <strong>[email protected]/strong></p><p>Your password is: <strong>123456</strong></p>'
};
transporter.sendMail (mailOptions, function (error, data) {
if (error) {
console. log("error accoure", error);
} else {
console. log( "email send success", data);
}});


  const start = async () => {
  try {
    await fastify.listen(3000);
    console.log('Server is running on port 3000');
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
};

start();

I'm using Glitch to run this project so please give me detailed instructions how I set up a SQL database. Thank you very much.

I have tried multiple solutions on the web but they all lead to errors.

0

There are 0 answers