'Conditionally serve ejs and react.js builds in smae url
I want to serve ejs and react.js conditionally to the user. When the user visits the page he would be landed to a login page which would be served from ejs. Once he logins in successfully which would be through an OAUTH, I want to serve the react.js build of the app.
const path = require("path");
const express = require("express");
const app = express(); // create express app
let ejs = require('ejs');
// app.get('/login', (req, res) =\> {
// app.set("view engine", "ejs");
// res.render('index');
// });
app.use('/', express.static(path.join(\__dirname, "..", "build"))); //React.js prod build path
app.use('/', express.static("public"));
app.get('\*', (req, res) =\> {
if(true) {
app.set("view engine", "ejs");
res.render('index');
} else {
res.sendFile("index.html", { root: path.join(\__dirname, "..", "build") });
}
});
// start express server on port 3000
app.listen(3000, () =\> {
console.log("server started on port 3000");
});
Any help regarding the serving of both apps would be highly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
