-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (44 loc) · 1.56 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
'use strict';
import 'dotenv/config.js';
import express from 'express';
import http from 'http';
import cors from 'cors';
import helmet from 'helmet';
import imageRoutes from './routes/imageRoutes.js';
import authRoutes from './routes/authRoutes.js';
import commentRoutes from './routes/commentRoutes.js';
import likeRoutes from './routes/likeRoutes.js';
import userRoutes from './routes/userRoutes.js';
const app = express();
const httpServer = http.createServer(app);
// Before routes; otherwise middleware didn't get called
if (process.env.NODE_ENV === 'production') {
app.enable('trust proxy');
app.use((req, res, next) => {
if (req.secure) {
// request was via https, so do no special handling
next();
} else {
// request was via http, so redirect to https
// if express app run under proxy with sub path URL
const proxypath = process.env.PROXY_PASS || '';
res.redirect(`https://${req.headers.host}${proxypath}${req.url}`);
}
});
app.listen(process.env.PORT);
} else {
httpServer.listen(process.env.PORT, () => {
console.log(`app listening on port ${process.env.PORT}`);
});
}
app.use(cors()); // To allow resources from different sources/outside domains
app.use(helmet()); // Protect your HTTP headers
app.use(express.static('public'));
app.use('/modules', express.static('node_modules'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/image', imageRoutes);
app.use('/auth', authRoutes);
app.use('/comment', commentRoutes);
app.use('/like', likeRoutes);
app.use('/user', userRoutes);