Guest User

Untitled

a guest
Feb 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const allowedExt = [
  2. '.js',
  3. '.ico',
  4. '.css',
  5. '.png',
  6. '.jpg',
  7. '.woff2',
  8. '.woff',
  9. '.ttf',
  10. '.svg',
  11. ];
  12.  
  13. const resolvePath = (file: string) => path.resolve(`../dist/${file}`);
  14.  
  15. @Middleware()
  16. export class FrontendMiddleware implements NestMiddleware {
  17. resolve(...args: any[]): ExpressMiddleware {
  18. return (req, res, next) => {
  19. const { url } = req;
  20. if (url.indexOf(ROUTE_PREFIX) === 1) {
  21. // it starts with /api --> continue with execution
  22. next();
  23. } else if (allowedExt.filter(ext => url.indexOf(ext) > 0).length > 0) {
  24. // it has a file extension --> resolve the file
  25. res.sendFile(resolvePath(url));
  26. } else {
  27. // in all other cases, redirect to the index.html!
  28. res.sendFile(resolvePath('index.html'));
  29. }
  30. };
  31. }
  32. }
Add Comment
Please, Sign In to add comment