1

This is my express server setting

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var app = express();

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


require('./api/accounts/signin')(app);
....

mongoose.connect(process.env.MONGO_URI);
mongoose.connection.on('error', function() {
    console.info('Error: Could not connect to MongoDB. Did you forget to run `mongod`?');
});

app.set('super-secret', process.env.APP_SECRET)

module.exports = app;

Now when i start my application at localhost:3000/ it works fine with all the routes working but when i try to go on localhost:3000/login or some other route directly i get a Cannot GET /login error...

I dont want to use server side rendering with a react middleware (which i have used before) just now so is there anyway to make it work?

app.get('/*/*', function(req, res) {
  res.sendFile(path.join( __dirname, 'public/index.html'))
})



<!doctype html>
<html>
  <head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>Doyouthink?</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="js/common.bundle.js"></script>
    <script src="js/polyfill.bundle.js"></script>
    <script src="js/vendor.bundle.js"></script>
  </body>

</html>

1 Answer 1

1

You just need to match all routes and serve them the base html page which includes your bundle. React Router will then examine and URL and render the appropriate components.

app.get("/", handleRender);
app.get("*", handleRender);

function handleRender(req, res){
  res.sendFile(__dirname + '/index.html');
}
Sign up to request clarification or add additional context in comments.

11 Comments

What will be handleRender here? isnt this a middleware for react in a sense aka server side rendering?
Sorry, edited. It just serves up your index html file.
Alright thank you so much... Also while we are at it, is it necessary (at some point in life) to plug in server side rendering in your single page application for making it SEO friendly and stuff like that or Im good to go without it?
It is mostly useful for SEO and people with slow connections. I'm fairly certain Google's bot will execute JavaScript, but other spiders won't necessarily correctly crawl your site. It isn't terribly difficult to setup, but I haven't read anything that says you must use it.
Hi i have run into a problem with this solution and im getting the html page as a response object from one of my API now which was working just fine before and sometimes the API are returning bad request. Is there something we missed?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.