tsx
98 lines
3 KB
| 1 | import express from 'express' |
| 2 | import React from 'react' |
| 3 | import ReactDOMServer from 'react-dom/server' |
| 4 | import App from '../src/components/App' |
| 5 | import path from 'path'; |
| 6 | import fs from 'fs'; |
| 7 | import { StaticRouter, matchPath } from 'react-router-dom'; |
| 8 | import routes, { Path } from '../src/shared/routes'; |
| 9 | import Firebase from '../src/components/Firebase'; |
| 10 | import firebaseInstance from '../src/components/Firebase/config'; |
| 11 | import { determineUserLang } from '../src/shared/i18n'; |
| 12 | |
| 13 | const server = express(); |
| 14 | |
| 15 | // Serve static files |
| 16 | server.use('/static', express.static('./build/static')); |
| 17 | server.use('/favicon.ico', express.static('./build/favicon.ico')); |
| 18 | server.use('/manifest.json', express.static('./build/manifest.json')); |
| 19 | server.use('/sitemap.xml', express.static('./build/sitemap.xml')); |
| 20 | |
| 21 | // Search console redirections |
| 22 | server.get('/songs-bridge/:path', (req, res) => { |
| 23 | res.redirect(301, `https://musik88.com/songs-bridge/${req.params.path}`); |
| 24 | }); |
| 25 | |
| 26 | server.get('/splitfire/:id', (req, res) => { |
| 27 | res.redirect(301, `https://splitfire.ai/`); |
| 28 | }); |
| 29 | |
| 30 | server.get('/*', (req, res, next) => { |
| 31 | |
| 32 | const acceptsLanguages = req.acceptsLanguages(); |
| 33 | console.log('SERVER#acceptsLanguages', acceptsLanguages); |
| 34 | const language = determineUserLang(acceptsLanguages); |
| 35 | console.log('SERVER#language', language); |
| 36 | |
| 37 | const activeRoute = routes.find((route) => { |
| 38 | const cleanUrl = req.url.split('?')[0]; |
| 39 | console.log('cleanUrl', cleanUrl); |
| 40 | return matchPath(route.path, cleanUrl) |
| 41 | }) |
| 42 | |
| 43 | const firebase = new Firebase(language, firebaseInstance.database()); |
| 44 | |
| 45 | if (!activeRoute) { |
| 46 | console.log('!activeRoute'); |
| 47 | return next(); |
| 48 | } |
| 49 | |
| 50 | // We have check the for validity of the path, |
| 51 | // so we can cast it to Path. |
| 52 | const ourPath: Path = req.path as Path; |
| 53 | console.log('req.path', req.path); |
| 54 | console.log('ourPath', ourPath); |
| 55 | |
| 56 | activeRoute |
| 57 | .fetchInitialData(ourPath, firebase) |
| 58 | .then((initialData) => { |
| 59 | |
| 60 | console.log('data', initialData); |
| 61 | console.log('data, stringify', JSON.stringify(initialData)); |
| 62 | |
| 63 | const app = ReactDOMServer.renderToString( |
| 64 | <StaticRouter location={req.url}> |
| 65 | <App initialData={initialData} /> |
| 66 | </StaticRouter> |
| 67 | ); |
| 68 | |
| 69 | const indexFile = path.resolve('./build/index.html'); |
| 70 | |
| 71 | fs.readFile(indexFile, 'utf8', (err, data) => { |
| 72 | if (err) { |
| 73 | console.error('Something went wrong:', err); |
| 74 | return res.status(500).send('Oops, better luck next time!'); |
| 75 | } |
| 76 | return res.send( |
| 77 | data |
| 78 | .replace( |
| 79 | '<div id="root"></div>', |
| 80 | `<div id="root">${app}</div>` |
| 81 | ) |
| 82 | .replace( |
| 83 | '<script id="initial-data"></script>', |
| 84 | `<script> |
| 85 | window.__DATA__=${JSON.stringify(initialData)} |
| 86 | </script>` |
| 87 | ) |
| 88 | ); |
| 89 | }); |
| 90 | }) |
| 91 | .catch(next); |
| 92 | }); |
| 93 | // Run on PORT 3002 unless SETOELKAHFI_COM_PORT is set |
| 94 | const port = process.env.SETOELKAHFI_COM_PORT || 3002; |
| 95 | |
| 96 | server.listen(port, () => { |
| 97 | console.log(`Server running on http://localhost:${port}`) |
| 98 | }) |