Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit fdef3fe

Browse files
Host static server
1 parent e105b69 commit fdef3fe

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

app-routes.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Configure all routes for express app
33
*/
4+
const express = require('express')
45
const _ = require('lodash')
56
const config = require('config')
67
const HttpStatus = require('http-status-codes')
@@ -115,6 +116,17 @@ module.exports = (app) => {
115116

116117
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc))
117118

119+
// Static file server
120+
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'development') {
121+
// Serve any static files
122+
app.use(express.static(path.join(__dirname, 'client/build')))
123+
124+
// Handle React routing, return all requests to React app
125+
app.get('*', (req, res) => {
126+
res.sendFile(path.join(__dirname, 'client/build', 'index.html'))
127+
})
128+
}
129+
118130
// Check if the route is not found or HTTP method is not supported
119131
app.use('*', (req, res) => {
120132
const route = routes[req.baseUrl]

app.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@ app.use((err, req, res, next) => {
8686
res.status(status).json(errorResponse)
8787
})
8888

89-
// Static file server
90-
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'development') {
91-
// Serve any static files
92-
app.use(express.static(path.join(__dirname, 'client/build')))
93-
94-
// Handle React routing, return all requests to React app
95-
app.get('*', (req, res) => {
96-
res.sendFile(path.join(__dirname, 'client/build', 'index.html'))
97-
})
98-
}
99-
10089
app.listen(app.get('port'), () => {
10190
logger.info(`Express server listening on port ${app.get('port')}`)
10291
})

0 commit comments

Comments
 (0)