docs: make 2.x the default version (#3369)
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
---
|
||||
description: "Set CORS HTTP headers with the cors middleware"
|
||||
---
|
||||
|
||||
The `cors()` middleware can be used to add
|
||||
[Cross-Origin-Resource-Sharing headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS)
|
||||
to HTTP requests. These allow the server to indicate which origins (domains,
|
||||
scheme or port) other than its own is permitted to load resources from.
|
||||
|
||||
```ts main.ts
|
||||
import { cors } from "fresh";
|
||||
|
||||
const app = new App()
|
||||
.use(cors({
|
||||
origin: "http://example.com",
|
||||
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
||||
allowMethods: ["POST", "GET", "OPTIONS"],
|
||||
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
||||
maxAge: 600,
|
||||
credentials: true,
|
||||
}))
|
||||
.get("/", () => new Response("hello"));
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
See the [API docs](https://jsr.io/@fresh/core/doc/~/cors) for a list of all
|
||||
supported options
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
description: "Prevent Cross-Site Request Forgery with this middleware"
|
||||
---
|
||||
|
||||
The `csrf()` middleware can be used to add safeguard against
|
||||
[Cross-Site Request Forgery vulnerabilities](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF).
|
||||
It checks if the user is allowed to load the requested URL based on the values
|
||||
in the
|
||||
[`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
|
||||
header and
|
||||
[`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
|
||||
header. to HTTP requests. These allow the server to indicate which origins
|
||||
(domains, scheme or port) other than its own is permitted to load resources
|
||||
from.
|
||||
|
||||
```ts main.ts
|
||||
import { app, csrf } from "fresh";
|
||||
|
||||
const app = new App();
|
||||
|
||||
app.use(csrf());
|
||||
|
||||
// Specify a single origin
|
||||
app.use(csrf({ origin: "https://example.com" }));
|
||||
|
||||
// Specify multiple origins
|
||||
app.use(
|
||||
csrf({ origin: ["https://example.com", "https://trusted.example.com"] }),
|
||||
);
|
||||
|
||||
// Use a function
|
||||
app.use(
|
||||
csrf({
|
||||
origin: (origin) => /^https:\/\/(foo|bar)\.example\.com$/.test(origin),
|
||||
}),
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
description: "Extend fresh with plugins"
|
||||
---
|
||||
|
||||
Fresh itself can be extended through the methods available on the
|
||||
[`App`](/docs/concepts/app) class or on the `Builder` class. Most of the
|
||||
features in Fresh itself are built using these APIs.
|
||||
|
||||
## Custom middlewares
|
||||
|
||||
If you need to modify requests, add HTTP headers or pass additional data to
|
||||
other middlewares via `ctx.state`, then going with a middleware is the way to
|
||||
go.
|
||||
|
||||
```ts middleware/fresh.ts
|
||||
const addXFreshHeader = define.middleware(async (ctx) => {
|
||||
const res = await ctx.next();
|
||||
res.headers.set("X-Fresh", "served by Fresh");
|
||||
return res;
|
||||
});
|
||||
```
|
||||
|
||||
Learn more about [middlewares](/docs/concepts/middleware).
|
||||
|
||||
TODO: Show more ways
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
description: "Ensure URLs always end or never end with trailing slashes"
|
||||
---
|
||||
|
||||
The `trailingSlashes()` middleware can be used to ensure URL pathnames always
|
||||
end with a slash character or will never end with one. It redirects the user's
|
||||
request respectively.
|
||||
|
||||
```ts main.ts
|
||||
import { trailingSlashes } from "fresh";
|
||||
|
||||
const app = new App()
|
||||
.use(trailingSlashes("never"))
|
||||
.get("/", () => new Response("hello"));
|
||||
```
|
||||
|
||||
Always append a trailing slash:
|
||||
|
||||
```ts main.ts
|
||||
import { trailingSlashes } from "fresh";
|
||||
|
||||
const app = new App()
|
||||
.use(trailingSlashes("always"))
|
||||
.get("/", () => new Response("hello"));
|
||||
```
|
||||
Reference in New Issue
Block a user