-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (39 loc) · 1.22 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
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import router from './routes/trans.routes.js'
import path from 'path';
import errhandle from './middlewares/error.js';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.resolve(__dirname, './public')));
app.use('/api', router);
app.get('/', (req, res) => {
res.send('Hello World!');
});
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`listening on port http://localhost:${port}`);
});
//Middleware for errors
app.use(errhandle);
//connect to mongodb
mongoose
.connect(process.env.DB_URL,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then((data) => {
console.log(`MongoDB connected with server: ${data.connection.host}`);
})
.catch((error) => {
console.log(`Error: ${error.message}`);
console.log(`Shutting down server due to unhandeled promise Rejection`);
server.close(() => {
process.exit(1);
});
});