下面是 nextjs 中 Route Handlers 的代码
export async function GET(request: Request) {
console.log(request)
console.log(request.method)
return Response.json({
message: 'hello world'
})
}
如果打印 request,会输出以下的信息
NextRequest [Request] {
[Symbol(realm)]: {
settingsObject: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] }
},
[Symbol(state)]: {
method: 'GET',
localURLsOnly: false,
unsafeRequest: false,
body: null,
client: { baseUrl: undefined, origin: [Getter], policyContainer: [Object] },
reservedClient: null,
replacesClientId: '',
window: 'client',
keepalive: false,
serviceWorkers: 'all',
initiator: '',
destination: '',
priority: null,
origin: 'client',
policyContainer: 'client',
referrer: 'client',
...
可以直接 console.log(request.method)
打印出 GET。那么像 [Symbol(state)] 这些属性怎么添加上去的,一般理解 [Symbol(state)] 应该是一个属性,那么访问 method 应该是 request[Symbol(state)].method
。如果给出一个普通对象,例如 {a: 5},那么怎样可以让 console.log() 输出类似的效果? 还有 [Request] 这些装饰是怎么添加进去的?