forked from mokshdoshi2005/Explainable-TRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (56 loc) · 1.9 KB
/
index.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
const express = require('express');
const mongoose = require('mongoose');
const User = require('./models/user'); // Import the User model
const app = express();
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }));
const mongodburl='mongodb+srv://admin:[email protected]/tourify'
mongoose.connect(mongodburl).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('Error connecting to MongoDB:', err);
});
// Middleware to parse JSON bodies
app.use(express.json());
// Serve HTML files from a directory named 'public'
app.use(express.static('public'));
app.post('/login', async (req, res) => {
// Implement admin signup logic
const {email,password} = req.body;
if (!req.body.email || !req.body.password) {
return res.status(400).json({ message: "Missing email or password" });
}
try {
const user = await User.find({
email
});
console.log(user);
if (user) {
return res.json({ message: "ok" });
}
else {
res.json("not a user");
}
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
// Route to handle user registration
app.post('/register', async (req, res) => {
try {
// Create a new user document based on the request body
const newUser = new User(req.body);
// Save the user to the database
await newUser.save();
res.status(201).send('User registered successfully');
} catch (error) {
console.error('Error registering user:', error);
res.status(500).send('Internal Server Error');
}
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}/login.html`);
});