-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (42 loc) · 1.32 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
// Import
const express = require("express");
const app = express();
const bcrypt = require("bcrypt"); // Importing bcrypt package
const users = [];
// Serve static files
app.use(express.static(__dirname + '/public'));
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded({ extended: true }));
// Route for home page
app.get('/', (req, res) => {
res.render("index.ejs")
})
app.get('#login', (req, res) => {
res.render("#login")
})
app.get('#register', (req, res) => {
res.render("#register")
})
// Route for registration form submission
app.post("#register", async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users.push({
id: Date.now().toString(),
name: req.body.name,
surname: req.body.surname,
email: req.body.email,
password: hashedPassword,
});
console.log(users); // Display newly registered user in the console
res.redirect("#login"); // Redirect to login after successful registration
} catch (e) {
console.log(e);
res.redirect("#register"); // Redirect back to the register page if there's an error
}
});
// Start the server
app.listen(3000, () => {
console.log("Server started on port 3000");
})
;