-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
73 lines (64 loc) · 1.82 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
66
67
68
69
70
71
72
73
// Import all packages
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const { success, error } = require('consola');
const passport = require('passport');
const path = require('path');
const methodOverride = require('method-override');
const config = require('config');
const DB = config.get('ATLAS_URI');
const { jwtPassport } = require('./Middlewares/passport');
// Initialize the application
const app = express();
const PORT = process.env.PORT || 8000;
// Packages middlewares
app.use(cors());
app.use(express.json());
app.use(passport.initialize());
app.use(methodOverride('_method'));
// User Defined Middlewares
jwtPassport(passport);
// Router middleware
app.use('/api/users', require('./Routes/registration'));
app.use('/api/users', require('./Routes/login'));
app.use('/api/profiles', require('./Routes/profile'));
app.use('/api/jobs', require('./Routes/jobPosts'));
app.use('/api/auth', require('./Routes/auth'));
//Serve static assets in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const connectandStart = async () => {
try {
// Connection with the database
await mongoose.connect(DB, {
useFindAndModify: false,
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
});
success({
message: 'Successfully connected with the MongoDB atlas',
badge: true,
});
// Listening to the port
app.listen(PORT, () =>
success({
message: `Server listening to port ${PORT}`,
badge: true,
})
);
} catch (err) {
error({
message: `Unable to connect to MongoDB atlas \n${err}`,
badge: true,
});
connectandStart();
}
};
connectandStart();