3

I've been trying to get this fixed for the last few hours but can't find something that works. What I'm using right now is the standard:

app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); });

I've tried to use the solution someone posted in another thread:

app.get(/*./, function (req, res) { res.sendFile(__dirname + '/index.html'); });

But this doesn't work and gives me a "Uncaught SyntaxError: Unexpected token <".

I guess that solution is based of expressjs own examples:

app.get(/*test$./, function (req, res) { res.sendFile(__dirname + '/index.html'); });

Which works for every route that has the phrase "test" in the first level (and ONLY the first lvl '/test'=yes, '/app/test'=no.

WHAT I WANT: Since I've a react app that always returns the same html page and that handles invalid routes within the app I want to make every request to my webpage return the exact same index.html (no matter what route the user asks for, '/' --> index.html '/test/test/test/test' --> index.html and so on.

Anyone have any ideas how I can do this? I've run out of google results

Thanks in advance!

Below is my entire server as of right now:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});

app.use('/build', express.static('build'));

io.on('connection', function(socket){
    console.log('Connected');
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});
2
  • have you tried using app.get('/*',...)'?, show us your react router config too. Commented May 7, 2016 at 8:54
  • 1
    I get the same, "Uncaught SyntaxError: Unexpected token <". The react routes works, I've tried to return the index.html file on different routes and react handles them as should. Commented May 7, 2016 at 9:02

3 Answers 3

2

If you just have app.use(yourStaticMiddleware), the function "yourStaticMiddleware" will be executed for every request. If that middleware ends the response (eg by calling res.send() or res.sendFile()), then nothing after it will be called except error handlers.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining, I believe thats what I was looking for. :-)
1

this will work for every get request

app.get("*",function(req,res,next){
  res.sendFile(__dirname + '/index.html');
})

1 Comment

Thanks for this anwser, it works just as well as the one above. When I tried it before I believe I forgot to add the next function (?), not sure if that was the issue. Thanks for taking the time :)
0

Use app.all() method.

This method is used for loading middleware functions at a path for all request methods.

app.all(function (req, res, next) {
   //will hit every page request regardless of http method
    next(); // pass control to the next handler
});

For more you can refer exprress routing

Comments

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.