Comments (9)
@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.
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.
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.
@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.
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.
@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.
@darrenjennings are you using express or fastify?
from serve-static.
Express
from serve-static.
Follow this example https://github.com/nestjs/nest/tree/master/sample/24-serve-static (it works here)
from serve-static.
Related Issues (20)
- serveRoot doesn't work with fastify platform HOT 3
- Get dynamic path
- After updating static-asset to 3.x, excluded paths fallback to index.html ( instead of returning a 404 ) HOT 2
- path-style versioning of static asset HOT 1
- Serve static can't handle globalprefix
- Nest can't resolve dependencies of the ServeStaticModule HOT 7
- Provide a documentation how to use setHeaders function
- Support `preCompressed` from `@fastify/static` HOT 1
- Cannot upgrade NestJS dependencies due to @fastify/static package HOT 1
- Not work if any controller in module has @Get('*') HOT 3
- Missing 'prefix' property in ServeStaticOptions HOT 1
- Action Required: Fix Renovate Configuration
- Error when set renderPath as regexp HOT 1
- serveStaticOptions: { index: false } doesn't work HOT 1
- Dependency Dashboard
- Watсh changes in static folder and reload webpage on change (like browsersync) HOT 1
- Module '"@nestjs/serve-static"' has no exported member 'ServeStaticModule'. HOT 1
- fastify-static is deprecated use @fastify/static instead HOT 1
- Support for nestjs v9 HOT 1
- Transform/Manipulate index html before sending it HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serve-static.