-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (48 loc) · 1.53 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
const PORT = process.env.PORT || 3001;
// MongoDB connection
mongoose.connect('mongodb+srv://prajwalkoppisettyhp:JPo0bvGk034AyFvq@voltagevortex.w6a93av.mongodb.net/voltage', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('MongoDB connected');
}).catch(err => console.log(err));
// Schema
const userSchema = new mongoose.Schema({
username: String,
email: String,
password: String
});
const User = mongoose.model('User', userSchema);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// Routes
app.post('/register', (req, res) => {
const { username, email, password } = req.body;
const newUser = new User({ username, email, password });
newUser.save()
.then(() => res.status(200).send('User registered successfully'))
.catch(err => res.status(500).send('Error registering user'));
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// Login route
app.post('/login', (req, res) => {
const { email, password } = req.body;
User.findOne({ email, password }) // No callback function here
.then(user => {
if (!user) {
res.status(401).send('Invalid credentials');
} else {
res.status(200).send('Login successful');
}
})
.catch(err => {
console.error(err);
res.status(500).send('Internal Server Error');
});
});