expressjs / serve-static

Serve static files
MIT License
1.38k stars 227 forks source link

Serve one file for any routes, catch all #105

Closed edencorbin closed 6 years ago

edencorbin commented 6 years ago

I'm probably missing something simple, but I am using this module to serve a react app, and I would like any address to serve one file index.html, here is the code I am using:

var serve=serveStatic('build', {'index': ['index.html', 'index.htm']})

var server = http.createServer(function onRequest (req, res) {
    serve(req, res, finalhandler(req, res))
})

It works fine for '/' but I want '/foo/bar to also load index.html, is that possible?

dougwilson commented 6 years ago

No, it's not currently possible. If someone wants to add it that would be good :+1:

Duplicate of #89

dougwilson commented 6 years ago

P.S. if that really is all your server is and you're not using Express or similar, you can accomplish what you want eaiser by just directly using the send module.

edencorbin commented 6 years ago

So I did leave some code out, essentially I was using this module to serve two separate directories, which I could not figure out how to do with serve (npm module) that is react-create-app default. It works, but unlike serve default behavior (that routes everything back to index.html and lets react handle the path) it tries to find exact files at specified route and delivers an error if it doesn't exist. I don't know the inner workings of how these modules differ. Looking into send now, interesting.

dougwilson commented 6 years ago

Hm. I can try to help figure out something that works, but need a description of the process you're trying to accomplish. The original issue was

I would like any address to serve one file index.html

Is that an accurate description of what you're looking to do? All URLs return the exact same index.html file's contents?

edencorbin commented 6 years ago

Well I thought that was what I wanted but to be honest I'm a bit confused. My issue is that with the serve module it requires assets to be in /build when serving the build folder. I would like my files to be located in another higher level folder, that is not part of the build process, so to accomplish this I am trying to serve two folders a /assets and a /build, running serve -s build, will allow me to have an arbitrary url to a file that doesn't exist, such as /bar/foo/nofilehere and it will still load .index and react will attempt to parse the route using its internal router module. I can serve two folders with serve-static, and it will properly load index.html in my /builds folder, but /bar/foo/nofilehere returns an error as the file doesn't exist. Sorry if my question is not phrased clearly, but I am not sure of how serve accomplishes allowing arbitrary routes to load index.html so react can parse them, which is essentially what I am trying to accomplish with serve-static or other server module.

Editing my comment: Looks like this is the key Serve option, anything similar for this module? -s, --single Serve single page apps with only one index.html

dougwilson commented 6 years ago

I'm not familiar off-hand with the serve module, so I'll need to research it to understand what those serve commands are actually doing to see how you'd accomplish them here 👍