mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-05 03:47:12 +00:00
37 lines
1005 B
Markdown
37 lines
1005 B
Markdown
---
|
|
title: 'setResponseStatus'
|
|
description: setResponseStatus sets the statusCode (and optionally the statusMessage) of the response.
|
|
links:
|
|
- label: Source
|
|
icon: i-simple-icons-github
|
|
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/ssr.ts
|
|
size: xs
|
|
---
|
|
|
|
Nuxt provides composables and utilities for first-class server-side-rendering support.
|
|
|
|
`setResponseStatus` sets the statusCode (and optionally the statusMessage) of the response.
|
|
|
|
::important
|
|
`setResponseStatus` can only be called in the [Nuxt context](/docs/guide/going-further/nuxt-app#the-nuxt-context).
|
|
::
|
|
|
|
```js
|
|
const event = useRequestEvent()
|
|
|
|
// event will be undefined in the browser
|
|
if (event) {
|
|
// Set the status code to 404 for a custom 404 page
|
|
setResponseStatus(event, 404)
|
|
|
|
// Set the status message as well
|
|
setResponseStatus(event, 404, 'Page Not Found')
|
|
}
|
|
```
|
|
|
|
::note
|
|
In the browser, `setResponseStatus` will have no effect.
|
|
::
|
|
|
|
:read-more{to="/docs/getting-started/error-handling"}
|