-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.4 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const ejs = require('ejs')
const path = require('path');
const port = 3000
const app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
mongoose.connect('mongodb+srv://sujal_don:[email protected]/?retryWrites=true&w=majority&appName=Cluster0',{
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB Atlas');
}).catch((err) => {
console.error('Error connecting to MongoDB Atlas:', err.message);
});
const userSchema = new mongoose.Schema({
username: String,
email: String,
password: String
});
const User = mongoose.model('User', userSchema);
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const newUser = new User({ username, password });
await newUser.save();
res.redirect('/index');
} catch (err) {
console.error('Error saving user:', err.message);
res.redirect('/');
}
});
app.get('/', (req,res)=>{
res.render('index')
})
app.get('/login-new', (req,res)=>{
res.render('login-new')
})
app.get('/index', (req,res)=>{
res.render('index')
})
app.listen(port, ()=>{
console.log('Server at 3000')
})