-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created a middleware to serve the frontend w/ react router
- Loading branch information
Showing
4 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters