-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (47 loc) · 1.13 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
// Requires
require('dotenv').config();
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const app = express();
// Import project dirs
const { apiRouter } = require('./api/index');
const { client } = require('./db/index');
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.use(cors());
// Catch-all route handler
app.get("/", (req, res) => {
res.send("Server is Running!")
});
// Router Handelers
app.use('/api', apiRouter)
try {
client.connect();
} catch (error) {
console.error("Unable to connect to database.", error);
process.exit(1);
};
// Close the database connection when the server stops
process.on('exit', () => {
console.log('Closing database connection');
client.end();
});
// Port
const PORT = process.env.PORT || 3001
if (process.env.NODE_ENV !== 'test') {
app.listen(PORT, () => {
console.log(`Now running on port ${PORT}`)
});
};
// Export
module.exports = {
app,
client,
jwt,
bcrypt
};