87 lines
3.3 KiB
TypeScript
87 lines
3.3 KiB
TypeScript
![]() |
// When to Use Server Hooks
|
||
|
|
||
|
// Server hooks are useful for:
|
||
|
// Logging: Log requests, responses, or errors.
|
||
|
// Authentication: Validate requests before they reach API routes.
|
||
|
// Modifying Requests/Responses: Add custom headers or transform data.
|
||
|
// Error Handling: Customize error responses or log errors.
|
||
|
// Resource Management: Clean up resources when the server shuts down.
|
||
|
|
||
|
export default defineNitroPlugin((nitroApp) => {
|
||
|
// Hook into the server lifecycle
|
||
|
nitroApp.hooks.hook("nitro:init", (nitro) => {
|
||
|
console.log("Nitro server is initializing!");
|
||
|
});
|
||
|
|
||
|
nitroApp.hooks.hook("request", (event) => {
|
||
|
console.log("Request received:", event.node.req.url);
|
||
|
});
|
||
|
|
||
|
nitroApp.hooks.hook("error", (error, event) => {
|
||
|
console.error("Error occurred:", error);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
// // 1. nitro:init
|
||
|
// // This hook is called when the Nitro server is being initialized. It allows you to modify the Nitro configuration or perform setup tasks before the server starts.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook("nitro:init", (nitro) => {
|
||
|
// console.log("Nitro server is initializing!");
|
||
|
// // Modify nitro options or perform setup tasks
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// // 2. nitro:config
|
||
|
// // This hook is called when the Nitro configuration is being generated. You can use it to modify the Nitro configuration before the server starts.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook("nitro:config", (config) => {
|
||
|
// console.log("Nitro config is being generated!");
|
||
|
// // Modify the Nitro config
|
||
|
// config.storage = {
|
||
|
// redis: {
|
||
|
// driver: "redis",
|
||
|
// /* Redis connection options */
|
||
|
// },
|
||
|
// };
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// 3. request
|
||
|
// This hook is called when a request is received by the server. You can use it to log requests, modify request data, or perform authentication checks.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook('request', (event) => {
|
||
|
// console.log('Request received:', event.node.req.url);
|
||
|
// // Modify the request or perform checks
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// 4. beforeResponse
|
||
|
// This hook is called before the server sends a response. You can use it to modify the response or add custom headers.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook('beforeResponse', (event, response) => {
|
||
|
// console.log('Sending response:', response);
|
||
|
// // Modify the response or add headers
|
||
|
// response.headers['X-Custom-Header'] = 'Hello from Nuxt!';
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// 5. error
|
||
|
// This hook is called when an error occurs during request handling. You can use it to log errors or customize error responses.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook('error', (error, event) => {
|
||
|
// console.error('Error occurred:', error);
|
||
|
// // Customize the error response
|
||
|
// event.node.res.statusCode = 500;
|
||
|
// event.node.res.end('Something went wrong!');
|
||
|
// });
|
||
|
// });
|
||
|
|
||
|
// 6. close
|
||
|
// This hook is called when the Nitro server is shutting down. You can use it to clean up resources or perform final tasks.
|
||
|
// export default defineNitroPlugin((nitroApp) => {
|
||
|
// nitroApp.hooks.hook('close', () => {
|
||
|
// console.log('Nitro server is shutting down!');
|
||
|
// // Clean up resources
|
||
|
// });
|
||
|
// });
|