-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
60 lines (44 loc) · 1.38 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
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const path = require('path');
const compression = require('compression');
const globalErrorHandler = require('./controllers/errorController');
const AppError = require('./utils/appError');
const userRoutes = require('./routes/userRoutes');
const authRoutes = require('./routes/authRoutes');
const jobRoutes = require('./routes/jobRoutes');
const app = express();
// implement CORS
app.use(cors());
app.options('*',cors());
// view engine
app.set('view engine', 'ejs');
// app.set('views', path.join(__dirname, 'views'));
// Development Logging
if(process.env.NODE_ENV === 'development'){
app.use(morgan('dev'));
}
// Body parser Middleware
app.use(express.json());
// compress all responses
app.use(compression());
// Use Routes
app.use('/api/users', userRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/job',jobRoutes);
// Serve static assets if 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'));
});
}
app.all('*', (req, res, next) => {
next(
new AppError(`Sorry! Can't find ${req.originalUrl} on this server!`, 404)
);
});
app.use(globalErrorHandler);
module.exports = app;