Skip to content

ServeStaticModule serves non-existing paths #224

Closed
@stovberpv

Description

@stovberpv

I'm submitting a...

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

Current behavior

When using the @nestjs/serve-static module, the server serves any path and does not return a 404 error even if the requested resource does not exist.

Project folder structure

.
├── client
│   └── index.hml
├── src
│   ├── app.module.ts

Input Code

// src\app.module.ts
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

imports: [
  ServeStaticModule.forRoot({ rootPath: join(process.cwd(), 'client') })
],

Testing

Request an existing resource

GET / HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK

Request a nonexistent resource

GET /foo HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 200 OK

Expected behavior

Request a nonexistent resource

GET /foo HTTP/1.1
Host: localhost:3000

Response

HTTP/1.1 404 Not Found

Minimal reproduction of the problem with instructions

  1. nest new testcase
  2. npm install --save @nestjs/serve-static
  3. Import ServeStaticModule.forRoot({ rootPath: join(process.cwd(), 'client') }) in app.module.ts
  4. Create folder testcase/client
  5. Create index file index.html under the testcase/client
  6. npm run start
  7. Make request at curl --location --request GET 'localhost:3000/foo'
  8. Got response HTTP/1.1 200 OK but expected HTTP/1.1 404 Not Found

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

In general, "serve-static" should follow the usual logic while serving files. If requested resource does not exists, an error is returned. Or in documentation should have a place where described this behavior and provided some instruction for handling that situations.

Environment

Nest version: 7.2.0
 
For Tooling issues:
- Node version: v12.16.3
- Platform: Windows

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions