Git Product home page Git Product logo

Comments (9)

darrenjennings avatar darrenjennings commented on April 28, 2024 13

@kamilmysliwiec I was able to get it to work with:

exclude: ['/api/(.*)'],

I looked at the regex returned from the path-to-regexp in the isRouteExcluded util. The latest version of the library (https://forbeslindesay.github.io/express-route-tester/) returns this regexp:

// '/api*'
^\/api\/(.*)\/?(?=\/|$)

but the code in my node_modules is returning which seems to disregard that a wildcard needs to be transformed:

// '/api*'
^\/api\/\*(?:\/(?=$))?$

So it could be some dependency version resolving to an older version as it seems my lockfile has many versions of path-to-regexp. Looks my local is resolving to 0.2.5.

from serve-static.

World3Developer avatar World3Developer commented on April 28, 2024 2

Please, change this example, because is not working.

https://github.com/nestjs/nest/blob/master/sample/24-serve-static/src/app.module.ts

from serve-static.

mishio-n avatar mishio-n commented on April 28, 2024

I think renderPath of ServeStaticModuleOptions can do this.

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client'),
      renderPath: '/',
    }),
  ],
})
export class ApplicationModule {}

If you set path for index.html to renderPath, index.html is only served when accessing your setting path.

In the above code,
localhost:3000 can only render index.html, and the others will return 404, for example localhost:3000/api.

from serve-static.

jannaahs avatar jannaahs commented on April 28, 2024

@mishio-n this does not solve the problem. It just shifts the problem to another case.

From my point of view the behavior can be described as follows:

Endpoints Expected Behavior Current Behavior @mishio-n Behavior
/ serves index.html serves index.html serves index.html
/api/not-found-route-goes-here JSON 404 Response serves index.html JSON 404 Response
/not-found-route-goes-here serves index.html serves index.html JSON 404 Response

The goal should be to return a 404 status code on not existing api endpoints. This would indicate possible problems like misspelling of the endpoint to the developers.

On the other hand the response of non api endpoints should always be the index.html so the SPA can decide how the 404 page should look like.

from serve-static.

kamilmysliwiec avatar kamilmysliwiec commented on April 28, 2024

The exclude option has been added in 2.0.0. See example here https://github.com/nestjs/nest/tree/master/sample/24-serve-static

from serve-static.

darrenjennings avatar darrenjennings commented on April 28, 2024

@kamilmysliwiec I just upgraded to 2.0.0, added the exclude but still seeing static files served:

ServeStaticModule.forRoot({
  rootPath: join(__dirname, '..', '..', 'client/public'),
  exclude: ['/api*'],

Expected:

▶ http :3000/api/this-should-404
HTTP/1.1 404 NOT FOUND

Acual:

▶ http :3000/api/this-should-404
GET /api/this-should-404 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
...
HTTP/1.1 200 OK
...
<!DOCTYPE html>
#<HTML OF STATIC INDEX.HTML HERE>

from serve-static.

kamilmysliwiec avatar kamilmysliwiec commented on April 28, 2024

@darrenjennings are you using express or fastify?

from serve-static.

darrenjennings avatar darrenjennings commented on April 28, 2024

Express

from serve-static.

kamilmysliwiec avatar kamilmysliwiec commented on April 28, 2024

Follow this example https://github.com/nestjs/nest/tree/master/sample/24-serve-static (it works here)

from serve-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.