-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (44 loc) · 1.33 KB
/
server.js
File metadata and controls
65 lines (44 loc) · 1.33 KB
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
60
61
62
63
64
65
import express from "express";
import dotenv from 'dotenv';
import mongoose from "mongoose";
import path from 'path';
import cors from 'cors';
import ejs from 'ejs';
import { fileURLToPath } from 'url';
import uploadRoutes from './routes/upload.routes.js'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config()
const app = express()
const PORT = process.env.PORT || 3000
// Connection to DB
mongoose.connect(process.env.MONGODB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true
}).then(() => {
console.log("MongoDB connected")
}).catch(err => {
console.error("MongoDB connectin error", err);
process.exit(1);
});
// ejs & staic config
app.engine('ejs', ejs.__express);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use('/public', express.static(path.join(__dirname, 'public')));
// middlewares
app.use(express.json());
app.use(cors());
// Routes
app.use('/', uploadRoutes);
// error handler
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({
message: err.message || "Server Error"
});
})
app.listen(PORT, () => {
console.log(`Server Listening on http://localhost:${PORT}`);
});