unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

Static FrontEnd (Angular, React, ...): New FileSystemProvider #533

Closed maarlo closed 2 years ago

maarlo commented 2 years ago

If serving both backend and frontend in the same server and creating the frontend with a javascript framework (like React), serving only the static files in basepath is not enough when the frontend has its own routing.

The server must serve static index.html file for all the unknown routes and files.

As solution, I propose modifying FileSystemProvider adding

  1. A readonly string set at constructor with the default document name (_defaultDocumentName)
  2. Modify the constructor to set that string
  3. Modify MapUrlPath return with
return !localPath.StartsWith(FileSystemPath, StringComparison.Ordinal) ? null
                : File.Exists(localPath) ? GetMappedFileInfo(mimeTypeProvider, localPath)
                : Directory.Exists(localPath) ? GetMappedDirectoryInfo(localPath)
                : GetMappedFileInfo(mimeTypeProvider, Path.Combine(FileSystemPath, _defaultDocumentName.Replace('/', Path.DirectorySeparatorChar)));

Instead of

return !localPath.StartsWith(FileSystemPath, StringComparison.Ordinal) ? null
                : File.Exists(localPath) ? GetMappedFileInfo(mimeTypeProvider, localPath)
                : Directory.Exists(localPath) ? GetMappedDirectoryInfo(localPath)
                : null;

To serve the files it's enough with adding

new WebServer(webServerOptions)
  ...
  .WithModule(new FileModule("/", new FrontendSystemProvider(HtmlRootPath, [defaultDocumentName, isImmutable])).WithContentCaching(UseFileCache));

What do you think about?

rdeago commented 2 years ago

Hello @maarlo, thanks for using EmbedIO!

Although I surely appreciate the effort you put in identifying the issue and providing a possible solution, I'm closing this issue for the following reasons:

  1. this is a duplicate of #490
  2. modifying FileSystemProvider will not solve the problem for people using other providers, e.g. ZipFileProvider, to store frontend files.

Please take a look at the solution proposed in #490 and tell us what you think.

maarlo commented 2 years ago

Hello @rdeago, thanks for your fast repply!

Yes, #490 is a better solution.