Skip to content

Commit

Permalink
created a middleware to serve the frontend w/ react router
Browse files Browse the repository at this point in the history
  • Loading branch information
nionata committed May 21, 2020
1 parent 04af6c3 commit 3530d86
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "client",
"version": "0.1.0",
"private": true,
"homepage": ".",
"homepage": "/frontend",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
Expand Down
13 changes: 13 additions & 0 deletions cms/middlewares/frontend/defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"frontend": {
"enabled": true,
"path": "./public/frontend",
"routes": [
"/",
"/login",
"/register",
"/teacher*",
"/workspace*"
]
}
}
30 changes: 30 additions & 0 deletions cms/middlewares/frontend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

/**
* Module dependencies
*/
const fs = require('fs')
const path = require('path')

module.exports = strapi => {
return {
/**
* Initialize the hook
*/

async initialize() {
// Configuration from middlewares/frontend/defaults.json
const { path: frontendPath, routes } = strapi.config.middleware.settings.frontend
const frontendDir = path.resolve(strapi.dir, frontendPath)

const serveFrontend = ctx => {
ctx.type = 'html'
ctx.body = fs.createReadStream(path.join(frontendDir + '/index.html'))
}

// Serve the index.html for all client routes
// This allows the react router to work properly
routes.forEach(path => strapi.router.get(path, serveFrontend))
},
}
}
3 changes: 2 additions & 1 deletion cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"strapi": "strapi",
"db-make": "docker run --name stem-c-db-dev -p 5432:5432 -e POSTGRES_DB=strapi -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -v /var/lib/postgresql/data -d postgres",
"db-start": "docker start stem-c-db-dev",
"db-bash": "docker exec -it stem-c-db-dev psql strapi postgres"
"db-bash": "docker exec -it stem-c-db-dev psql strapi postgres",
"build-frontend": "rm -rf ./public/frontend/* && cd ../client && yarn build && mv ./build/* ../cms/public/frontend"
},
"devDependencies": {},
"dependencies": {
Expand Down

0 comments on commit 3530d86

Please sign in to comment.