I have been tasked with creating a small database of users for someone, and I'm using MySQL alongside with Node.js in order to do it My idea was pretty simple - a form in a .ejs file, in it 2 parameters - username and passwords, and when someone enters the details, in an app.post(), it inserts the entered information from the form into the database and sends the user to a temporary "Done Successfully" site. Here is the part of the code that matters:
The form:
<form class="form-signup" action="/successful" target="_blank" method="post" enctype="multipart/form-data">
<label for="username">Username:</label>
<input type="text" name="username" placeholder="Type your username" required>
<br>
<label for="password">Password:</label>
<input type="password" name="password" placeholder="Type your password" required>
<br>
<button class="center" type="submit">Sign Up</button>
</form>
app.js:
const express = require('express');
const body_parser = require('body-parser');
const mysql = require('mysql');
const app = express();
app.set('view engine', 'ejs');
app.use(body_parser.urlencoded({extended: true}));
app.use(body_parser.json());
app.listen('3000', function() {
console.log('Server is running at localhost:3000');
});
...
app.post('/successful', function (req, res) {
let user = {
username: req.body.username,
password: req.body.password,
created: new Date()
}
let sql = "INSERT INTO usernames SET ?";
let query = db.query(sql, user, function (err, result) {
if (err) throw err;
else console.log(req.body);
});
res.status(200).render('successful');
});
(I did connect the database, just didn't show it)
And yet req.body returns {}. I would appreciate any amount of help. Thanks in advance!