Response
Append a response header by name.
Example:
Append the response headers.
Example:
Append a response header by name.
Example:
Append the response headers.
Example:
Remove all response headers, or only those specified in the headerNames array.
Example:
Set the response status code and message.
Alias for getResponseHeaders
.
Example:
Get the response headers object.
Example:
Get the current response status code.
Example:
Get the current response status message.
Example:
Checks if the data is a stream. (Node.js Readable Stream, React Pipeable Stream, or Web Stream)
Checks if the data is a Response object.
Remove a response header by name.
Example:
Directly send a response to the client.
Note: This function should be used only when you want to send a response directly without using the h3
event. Normally you can directly return
a value inside event handlers.
Iterate a source of chunks and send back each chunk in order. Supports mixing async work together with emitting chunks.
Each chunk must be a string or a buffer.
For generator (yielding) functions, the returned value is treated the same as yielded values.
Example:
Respond with an empty payload.
Note that calling this function will close the connection and no other data can be sent to the client afterwards.
Example:
Send a redirect response to the client.
It adds the location
header to the response and sets the status code to 302 by default.
In the body, it sends a simple HTML page with a meta refresh tag to redirect the client in case the headers are ignored.
Example:
Send a stream response to the client.
Note: You can directly return
a stream value inside event handlers alternatively which is recommended.
Send a Response object to the client.
Set a response header by name.
Example:
Set the response headers.
Example:
Set a response header by name.
Example:
Set the response headers.
Example:
Set the response status code and message.
Example:
Write HTTP/1.1 103 Early Hints
to the client.
export default defineEventHandler((event) => {
appendResponseHeader(event, "content-type", "text/html");
});
export default defineEventHandler((event) => {
appendResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
export default defineEventHandler((event) => {
appendResponseHeader(event, "content-type", "text/html");
});
export default defineEventHandler((event) => {
appendResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
export default defineEventHandler((event) => {
clearResponseHeaders(event, ["content-type", "cache-control"]); // Remove content-type and cache-control headers
});
export default defineEventHandler((event) => {
const contentType = getResponseHeader(event, "content-type"); // Get the response content-type header
});
export default defineEventHandler((event) => {
const headers = getResponseHeaders(event);
});
export default defineEventHandler((event) => {
const status = getResponseStatus(event);
return `Status: ${status}`;
});
export default defineEventHandler((event) => {
const statusText = getResponseStatusText(event);
return `Status: ${statusText}`;
});
export default defineEventHandler((event) => {
removeResponseHeader(event, "content-type"); // Remove content-type header
});
sendIterable(event, work());
async function* work() {
// Open document body
yield "<!DOCTYPE html>\n<html><body><h1>Executing...</h1><ol>\n";
// Do work ...
for (let i = 0; i < 1000) {
await delay(1000);
// Report progress
yield `<li>Completed job #`;
yield i;
yield `</li>\n`;
}
// Close out the report
return `</ol></body></html>`;
}
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export default defineEventHandler((event) => {
return sendNoContent(event);
});
Example:
export default defineEventHandler((event) => {
sendNoContent(event); // Close the connection
console.log("This will not be executed");
});
export default defineEventHandler((event) => {
return sendRedirect(event, "https://example.com");
});
Example:
export default defineEventHandler((event) => {
return sendRedirect(event, "https://example.com", 301); // Permanent redirect
});
export default defineEventHandler((event) => {
setResponseHeader(event, "content-type", "text/html");
});
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
export default defineEventHandler((event) => {
setResponseHeader(event, "content-type", "text/html");
});
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
export default defineEventHandler((event) => {
setResponseStatus(event, 404, "Not Found");
return "Not Found";
});