-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (41 loc) · 1.29 KB
/
Copy pathindex.js
File metadata and controls
50 lines (41 loc) · 1.29 KB
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
const express = require("express");
const path = require("path");
const session = require("express-session");
const app = express();
const registerUser = require("./Controller/register");
const PORT = 3000;
// ✅ Add this middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Configure session middleware
app.use(session({
secret: "your-secret-key",
resave: false,
saveUninitialized: true,
cookie: { maxAge: 3600000 }
}));
app.use(express.static(path.join(__dirname, 'pages')));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'pages', 'index.html'));
});
app.post("/submit", registerUser);
// Example route for login
app.post("/login", (req, res) => {
const { email, password } = req.body;
if (email === "buyer@example.com" && password === "password123") {
req.session.user = { email, role: "buyer" };
res.send("Login successful");
} else {
res.status(401).send("Invalid credentials");
}
});
// Example route for logout
app.post("/logout", (req, res) => {
req.session.destroy(err => {
if (err) return res.status(500).send("Logout failed");
res.send("Logout successful");
});
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});