-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
43 lines (34 loc) · 946 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from "next/server";
// CORS
const allowedOrigins =
process.env.NODE_ENV === "production"
? ["https://www.yoursite.com", "https://yoursite.com"]
: ["http://localhost:3000"];
export function middleware(request: Request) {
// 1
// if (request.url.includes("/api/")) {
// }
// 2
// const regex = new RegExp("/api/*");
// if (regex.test(request.url)) {
// }
const origin = request.headers.get("origin");
console.log("\"origin\":",origin);
// Next.js default configuration is to allow all OPTION request, 204
if (origin && !allowedOrigins.includes(origin)) {
return new NextResponse(null, {
status: 400,
statusText: "Bad Request",
headers: {
"Content-Type": "text/plain",
},
});
}
console.log("Middleware!");
console.log(request.method, request.url);
return NextResponse.next();
}
// 3
export const config = {
matcher: "/api/:path*",
};