forked from Note-Vault/Note-Vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (41 loc) · 1.32 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
import express from "express";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import bodyParser from "body-parser";
import { config as configDotenv } from "dotenv"; // Use config method from dotenv
import userRoutes from './routes/user.js';
import staticRoutes from './routes/staticRoutes.js';
import NotebookRoutes from './routes/notebook.js';
configDotenv();
mongoose
.connect(process.env.MONGODB)
.then(() => {
console.log("Connected to MongoDB");
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
});
const app = express();
const port = 3000; // Change this to the desired port number
// Middleware to read the body data in json format
app.use(express.json());
// Middleware to parse cookies
app.use(cookieParser());
// Using EJS as viewEngine
app.set("view engine", "ejs");
// Parse URL-encoded bodies
app.use(bodyParser.urlencoded({ extended: false }));
// Serve static files from the 'public' folder
app.use(express.static("public"));
//Routes
app.use('/',staticRoutes);
app.use('/',userRoutes);
app.use('/',NotebookRoutes);
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
// Custom error handler for 404 Not Found
app.use((req, res, next) => {
res.status(404).render('404', { title: 'Page Not Found' });
});