-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.45 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
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const cookieParser = require('cookie-parser');
const userRoute = require('./Routes/user');
const blogRoute = require('./Routes/blog');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const mongoose = require('mongoose');
const { authenticationMiddleware } = require('./middlewares/authMiddleware');
mongoose.connect('mongodb://127.0.0.1:27017/NodeBlog')
.then(() => {
console.log("MongoDb Connected Succesfuly");
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
});
const app = express();
const PORT = 8000;
app.set('view engine', 'ejs');
app.set('views', path.resolve('./Views'));
// Middleware for parsing JSON and urlencoded data
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(upload.single('coverImage'));
//middleware for parsing cookies
app.use(cookieParser());
//our middleware
app.use(authenticationMiddleware("token"));
// Routes
app.get('/', (req, res) => {
res.render('Home',
{
user:req.user
});
});
// User routes
app.use('/user', userRoute);
app.use('/blog', blogRoute);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Internal Server Error');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server started at Port:${PORT}`);
});