-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
72 lines (62 loc) · 2.01 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
61
62
63
64
65
66
67
68
69
70
71
72
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const childProcess = require('child_process');
const morgan = require('morgan');
const db = require('./db.js');
const users = require('./routes/api/users');
const products = require('./routes/api/products');
const admins = require('./routes/api/admins');
const storages = require('./routes/api/storages');
const inventories = require('./routes/api/inventories');
const carts = require('./routes/api/carts');
const purchases = require('./routes/api/purchases');
const staticsPath = require('./config/storage').staticsPath;
const app = express();
console.log('NODE_ENV: ' + process.env.NODE_ENV);
//log using morgan
if (process.env.NODE_ENV !== "TRAVIS" &&
process.env.NODE_ENV !== "TEST" && process.env.NODE_ENV !== "PRODUCTION") {
app.use(morgan('dev'));
}
//Set CORS headers
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH"
);
next();
});
//Serve static images
app.use('/images', express.static(path.join(staticsPath, 'images', 'full')));
//body parser middleware
app.use(bodyParser.urlencoded({
extended: false,
limit: '5mb'
}));
app.use(bodyParser.json({
limit: '5mb'
}));
//passport middleware
app.use(passport.initialize());
//passport config
require('./config/passport')(passport);
//User Routes
app.use('/api/users', users);
app.use('/api/products', products);
app.use('/api/admins', admins);
app.use('/api/storages', storages);
app.use('/api/inventories', inventories);
app.use('/api/carts', carts);
app.use('/api/purchases', purchases);
app.use(express.static(path.join(__dirname, 'client', 'build')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
module.exports = app;