-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (38 loc) · 1.62 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const hbs = require('hbs');
const authMiddleware = require('./middleware/authMiddleware'); // Custom authentication middleware
require('dotenv').config();
const app = express();
const DB='mongodb+srv://rajvardhanchaudhary666:[email protected]/user-registration-app'
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
// Connect to MongoDB
// mongoose.connect('mongodb://localhost:27017/user-registration-app')
// .then(() => console.log('Connected to MongoDB'))
// .catch(err => console.error('Failed to connect to MongoDB', err));
mongoose.connect(DB)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Failed to connect to MongoDB', err));
// Routes
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/users');
app.use('/api/auth', authRoutes);
app.use('/api/users', authMiddleware, userRoutes); // Protect this route with authMiddleware
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Views routes
app.get('/', (req, res) => res.render('register')); // Registration page
app.get('/login', (req, res) => res.render('login')); // Login page
app.get('/home', (req, res) => res.render('home')); // Home page, protected
// Start server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});