question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is not good way to catch "File not found" errors on static assets

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

So if you setup the ServeStaticModule and provide serveStaticOptions: { fallthrough: false }, the module reports a 500 (Internal Server) error.

I don’t know why this is not coming clearly to me, but I want to change the 500 error to a 404, because the file was not found.

I tried to use a global Internal Server filter and match the route in the case of 500 errors, but that wasn’t possible because for some reason the filter doesn’t get hit. I must be missing something higher level about NestJS.

Expected behavior

I expect there to be an ergonomic way to handle errors when fallthrough is set to false.

Minimal reproduction of the problem with instructions

Use the example in the nest repo: https://github.com/nestjs/nest/blob/master/sample/24-serve-static/src/app.module.ts#L8

and configure your ServeStaticModule to also serve your home directory (just an example) include serveStaticOptions: { fallthrough: false }. Then after you have set up the mount point, start the server and enter a URL under the mount point that would not exist in your home directory. Your browser should report a 500 error.

What is the motivation / use case for changing the behavior?

It currently seems either impossible or not easily discoverable on how a developer can catch the serve-static 500 error and change it to a 404 for when a file is not found.

Environment


Nest version: 7.0.2



Honestly maybe this is an issue for the serve-static repo. I am unsure.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
raphaelsoulcommented, Mar 25, 2021

Is it possible to disable index fallback to avoid this exception?

ServeStaticModule.forRoot({
      rootPath: path.join(__dirname, '../../../public'),
      serveStaticOptions: {
        index: false,
      },
    }),
[2021-03-25T10:35:11.080] [INFO] NestApplication - Nest application successfully started
[2021-03-25T10:35:13.787] [ERROR] ExceptionsHandler - ENOENT: no such file or directory, stat '/Users/raphaelsoul/projects/zuma/nodejs/mock-server/public/index.html' Error: ENOENT: no such file or directory, stat '/Users/raphaelsoul/projects/zuma/nodejs/mock-server/public/index.html'
[2021-03-25T10:35:13.788] [ERROR] ExceptionsHandler - ENOENT: no such file or directory, stat '/Users/raphaelsoul/projects/zuma/nodejs/mock-server/public/index.html' Error: ENOENT: no such file or directory, stat '/Users/raphaelsoul/projects/zuma/nodejs/mock-server/public/index.html'
3reactions
raphaelsoulcommented, Mar 10, 2022

https://github.com/nestjs/serve-static/blob/d0f4a89ca32a7d2315b03a045d7a57bc99f11771/lib/loaders/express.loader.ts#L44-L59

res.sendFile in renderFn causes no such file error.

I think it is the best practice. we dont need a global exeception filter

ServeStaticModule.forRoot({
      rootPath: resolve(__dirname, '../public'),
      serveRoot: '/',
}),

For SPAs it is never a problem cause you will always have an index.html(or using index specify a filename)

For serving static file in traditional frontend project, just give a non-empty serveRoot.

Therefore, it seems there is no need for a PR to fix this, we just need update nestjs docs to declare usage for serving SPAs and non-SPAs @kamilmysliwiec

#207 #523

UPDATE:

in some occasions, the solution above(give non empty serveRoot) still not works. For Example, you want to serve skins/material-design/assets under /themes/material-design.😆

ServeStaticModule.forRoot({
      rootPath: resolve(__dirname, '../skins/material-design/assets'),
      serveRoot: '/themes/material-design',
})

code below does not throw exceptions to built-in ExceptionsHandler class

ServeStaticModule.forRoot({
      rootPath: resolve(__dirname, '../skins/material-design/assets'),
      serveRoot: '/themes/material-design/', // NOTE: it end with slash `/`
})

you can check the reason here(for express) image

every request url start serveRoot will be handled by express static middleware in line38 in the screenshot instead of renderFn function

It is very tricky. Seems we need robust code in @nestjs/serve-static library

Read more comments on GitHub >

github_iconTop Results From Across the Web

static files not found error in django - Stack Overflow
Create folder static_files in your app directory. And place all your static files inside it. Then use the following settings
Read more >
How to Handle the FileNotFoundException in C# | Rollbar
The FileNotFoundException occurs in C# when the system is unable to locate a file when the program is executed. Learn how to handle...
Read more >
The Asset Pipeline - Ruby on Rails Guides
Raise an Error When an Asset is Not Found; Turning Digests Off; Turning Source Maps On ... For CSS files, this is done...
Read more >
Fix error messages with Uploaded ads - Google Ads Help
ZIP file is called path/to/example/asset, but in your HTML file you reference path/to/example/asset2, Google Ads will not be able to find the correct...
Read more >
Exception handling - Knowledge Kitchen
The following programs cause a stack overflow error to occur, ... The following examples show how to handle a typical 'file not found'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found