-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (61 loc) · 2.09 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require("express")
const app = express();
const mysql = require('mysql2')
const dotenv = require('dotenv')
const path = require('path');
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use(express.json());
app.use(express.static('FSWD-PROJECT')); // Replace 'public' with your actual directory
dotenv.config()
const pool = mysql.createPool({
host : process.env.MYSQL_HOST,
user :process.env.MYSQL_USER,
password:process.env.MYSQL_PASSWORD,
database:process.env.MYSQL_DATABASE
}).promise()
async function createNote(username,email,dob,password){
const [result] = await pool.query("INSERT INTO users (username, email, dob, password)VALUES (?,?,?,?)",[username,email,dob,password])
return {id:result.insertId,username, email, dob, password}
}
app.get('/', (req, res) => {
res.render("index.ejs");
});
app.get('/login', (req, res) => {
res.render("login.ejs");
});
app.get('/registration', (req, res) => {
res.render("registration.ejs");
});
app.get('/habits', (req, res) => {
res.render("habits.ejs");
});
app.post('/register', async (req, res) => {
const { username, email, dob, password } = req.body;
try {
const newUser = await createNote(username, email, dob, password);
res.json(newUser);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/login', async (req, res) => {
const { email, password } = req.body;
try {
// Retrieve user from the database based on the email
const [rows] = await pool.query('SELECT * FROM users WHERE email = ?', [email]);
if (rows.length === 1 && rows[0].password === password) {
// Successful login
res.sendStatus(200);
} else {
// Invalid credentials
res.status(401).send('Invalid email or password');
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(3000, () => {
console.log("Server is running on port 3000!!");
});