Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Plugins let you hook into Nitro's runtime lifecycle. This example shows a plugin that modifies the Content-Type header on every response. Create files in server/plugins/ and they're automatically loaded at startup.

Defining a Plugin

import { definePlugin } from "nitro";
import { useNitroHooks } from "nitro/app";

export default definePlugin((nitroApp) => {
  const hooks = useNitroHooks();
  hooks.hook("response", (event) => {
    event.headers.set("content-type", "html; charset=utf-8");
  });
});

The plugin uses useNitroHooks() to access the hooks system, then registers a response hook that runs after every request. Here it sets the content type to HTML, but you could log requests, add security headers, or modify responses in any way.

Main Handler

import { defineHandler } from "nitro";

export default defineHandler(() => "<h1>Hello Nitro!</h1>");

The handler returns HTML without setting a content type. The plugin automatically adds the correct Content-Type: html; charset=utf-8 header to the response.