-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
65 lines (54 loc) · 1.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require('dotenv').config();
const cors = require('cors');
const morgan = require('morgan');
const express = require('express');
const mongoose = require('mongoose');
const httpStatus = require('http-status');
const viewRoute = require('./routes/view.route');
const authRoute = require('./routes/auth.route');
const userRoute = require('./routes/user.route');
const classRoute = require('./routes/class.route');
const upload = require('./middlewares/multer.middleware');
const errorHandler = require('./middlewares/error.middleware');
const app = express();
const port = process.env.PORT || 3000;
const mongoURI = process.env.MONGO_URI || 'mongodb://localhost:27017/hit-nodejs-2024';
app.use(cors());
app.use(express.json());
app.set('views', './views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use('/uploads', express.static('uploads'));
app.use(morgan('dev'));
// view
app.use('/auth', viewRoute);
app.use('/api/v1/auth', authRoute);
app.use('/api/v1/users', userRoute);
app.use('/api/v1/classes', classRoute);
app.post('/uploads', upload.single('file'), (req, res) => {
const urlPublic = `http://localhost:${port}/uploads/${req.file.filename}`;
res.send({
message: 'File uploaded successfully',
urlPublic,
});
});
app.all('*', (req, res) => {
res.status(httpStatus.NOT_FOUND).send({
message: 'Not found',
code: httpStatus.NOT_FOUND,
});
});
app.use(errorHandler);
mongoose
.connect(mongoURI)
.then(() => {
console.log('MongoDB connected');
})
.then(() => {
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
})
.catch((err) => {
console.log(err);
});