-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (39 loc) · 1.29 KB
/
app.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
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 4000;
const tasks = require('./routes/tasks');
const connectDB = require('./db/connect');
const notFound = require('./middlewares/not-found');
const errorHandlerMiddleware = require('./middlewares/error-handler');
require('dotenv').config();
app.use(express.static(__dirname + '/public/'));
// middlewares
app.use(express.json()); // to have data in req.body
// routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + "/views", '/index.html'));
});
app.get('/edit-task', (req, res) => {
res.sendFile(path.join(__dirname + "/views", '/task.html'));
});
app.use('/api/v1/tasks', tasks);
// app.get('/api/v1/tasks') - get all tasks
// app.post('/api/v1/tasks') - create a new task
// app.get('/api/v1/tasks/:id') - get a single task
// app.patch('/api/v1/tasks/:id') - update a task
// app.delete('/api/v1/tasks/:id') - delete a task
app.use(notFound);
app.use(errorHandlerMiddleware);
const start = async () => {
try {
await connectDB(process.env.MONGO_URI);
// Start the web server
app.listen(port, () => {
console.log(`Listening on port:${port}`);
});
} catch (error) {
console.log(error);
}
}
start();