This article explains how you can configure routes in a SPA project deployed at our Cloud Code.
We prepared many documentations showing how to configure Ionic, Angular or React, a detailed guide can be found in our documentation.
To configure the routing to your SPA in Express, you should use the GET HTTP method to include an HTML file and in the response, we are going to deliver the index.html file to the browser.
const path = require('path');
const unRoutedPaths = [
'apps',
'batch',
'requestPasswordReset',
'files',
'login',
'logout',
'user',
'users',
'Roles',
'parse',
'schemas',
'functions',
'classes',
'aggregate',
'cloud_code',
'config',
'hooks',
'push_audiences',
'installations',
'push',
'sessions',
'events',
'jobs',
'export_progress',
'export_data',
'graphql',
'import_data',
'health',
'purge',
'scriptlog'
];
app.use((req, res, next) => {
const pathParts = req.path.split('/').filter((p) => p !== '');
if (req.path.indexOf('.') > 0 || unRoutedPaths.includes(pathParts[0])) {
next();
} else {
res.sendFile(path.join(`${__dirname}/public/index.html`));
}
});
Note: Include the code above in a file called app.js and deploy it in the cloud folder in the Cloud Code section.
Comments
0 comments
Please sign in to leave a comment.