2022-10-06 09:15:30 +00:00
|
|
|
---
|
2023-10-18 10:59:43 +00:00
|
|
|
title: 'setResponseStatus'
|
2022-10-06 09:15:30 +00:00
|
|
|
description: setResponseStatus sets the statusCode (and optionally the statusMessage) of the response.
|
2023-10-18 10:59:43 +00:00
|
|
|
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
|
2022-10-06 09:15:30 +00:00
|
|
|
---
|
2022-08-02 16:01:59 +00:00
|
|
|
|
|
|
|
Nuxt provides composables and utilities for first-class server-side-rendering support.
|
|
|
|
|
2022-10-06 09:15:30 +00:00
|
|
|
`setResponseStatus` sets the statusCode (and optionally the statusMessage) of the response.
|
2022-08-02 16:01:59 +00:00
|
|
|
|
2024-02-21 17:09:45 +00:00
|
|
|
::important
|
2024-01-31 09:46:21 +00:00
|
|
|
`setResponseStatus` can only be called in the [Nuxt context](/docs/guide/going-further/nuxt-app#the-nuxt-context).
|
|
|
|
::
|
|
|
|
|
2022-08-02 16:01:59 +00:00
|
|
|
```js
|
2023-03-31 14:02:26 +00:00
|
|
|
const event = useRequestEvent()
|
|
|
|
|
2024-01-29 11:48:35 +00:00
|
|
|
// event will be undefined in the browser
|
|
|
|
if (event) {
|
|
|
|
// Set the status code to 404 for a custom 404 page
|
|
|
|
setResponseStatus(event, 404)
|
2022-08-02 16:01:59 +00:00
|
|
|
|
2024-01-29 11:48:35 +00:00
|
|
|
// Set the status message as well
|
|
|
|
setResponseStatus(event, 404, 'Page Not Found')
|
|
|
|
}
|
2022-08-02 16:01:59 +00:00
|
|
|
```
|
|
|
|
|
2024-02-21 17:09:45 +00:00
|
|
|
::note
|
2022-08-02 16:01:59 +00:00
|
|
|
In the browser, `setResponseStatus` will have no effect.
|
|
|
|
::
|
2023-10-18 10:59:43 +00:00
|
|
|
|
|
|
|
:read-more{to="/docs/getting-started/error-handling"}
|