Typeerror: res.status is not a function

Are you having a hard time dealing with this “typeerror: res.status is not a function” error message?

In this article, we will explore the different solutions to fix the “res.status is not a function” type error.

Aside from that, we’ll also discuss what this error means and why it occurs in your code.

What is “typeerror: res.status is not a function”?

The “typeerror: res.status is not a function” is an error message that occurs when you are working with Node.js and Express.

It occurs when you are trying to set the HTTP status code for a response object in Express.

It indicates that the “status” method is not recognized as a function of the res object.

In addition to that, this error usually happens if the “res” object has been overwritten or if it is not properly defined.

Why does this “res.status is not a function” error message occur?

There are several reasons why this error occurs in your code, including:

  • If you’re using the incorrect syntax when calling the res.status() method.
  • Using the incorrect use of middleware in the Express application.
  • If you incorrect use of callback functions in the Express application.

What is Node.js?

Node.js is an open-source server environment that allows you to run JavaScript on the server side.

Also, Node.js provides built-in modules that you can use in your applications, such as the “http” and “fs” modules.

It’s perfect for building scalable and high-performance web applications.

What is Express?

Express is a popular and lightweight framework for building web applications with Node.js.

That provides a robust set of features for web and mobile applications.

In addition to that, it provides a set of features for handling HTTP requests and responses, defining routes, and managing middleware functions.

How to fix “typeerror: res.status is not a function”?

To fix this type error, “res.status is not a function”, you have to make sure that the “res” object has the “status()” function defined on it.

To do this, you have to ensure that you are using a web framework or library that provides this function, such as Express.js. 

Another way is to check your code to make sure that you are correctly defining the res object and that it has the necessary functions and properties defined on it.

Here are the following solutions you may use to fix “response.status is not a function.”

1: Check if you are using a web framework or library that provides the res.status() function.

Here is a sample code that shows how to use the res.status() function in Express.js:

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.status(200).send('Hello, Welcome to Itsourcecode!')
})

app.listen(3000, () => {
  console.log('Server listening on port 3000...')
})

As you can see in this solution, it uses the res.status() function provided by the Express.js framework to set the HTTP status code of the response.

In this case, we are setting it to 200 for a successful response.

The send() function sends the response body to the client.

Here’s the result of this code when you run it and make a GET request to the root path:

$ node app.js
Server listening on port 3000...

$ curl http://localhost:3000
Hello, Welcome to Itsourcecode!

2: Use Node.js built-in http module

You can use the Node.js built-in http module to create a server and handle HTTP requests.


The res.writeHead() function sets the HTTP status code of the response.


In this case, we are setting it to 200 for a successful response.


The res.end() function sends the response to the client.

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200);
  res.end('Hello, Welcome to Itsourcecode!');
});

server.listen(3000, () => {
  console.log('Server listening on port 3000...');
});

3: Use http-errors module

You can use the http-errors module to create a custom HTTP error with a status code of 200.

This allows you to use the res.status() function to set the HTTP status code of the response.

const createError = require('http-errors');

const app = express();

app.get('/', (req, res, next) => {
  next(createError(200, 'Hello, Welcome to Itsourcecode!'));
});

app.use((err, req, res, next) => {
  res.status(err.status || 500);
  res.send(err.message);
});

app.listen(3000, () => {
  console.log('Server listening on port 3000...');
});

4: Use custom response object

As you can see in this solution, it creates a custom response object that defines the status() and send() functions.


Then use the custom response object to set the HTTP status code of the response and send the response body.

class CustomResponse {
  constructor(res) {
    this.res = res;
  }

  status(statusCode) {
    this.res.statusCode = statusCode;
    return this;
  }

  send(data) {
    this.res.end(data);
  }
}

const server = http.createServer((req, res) => {
  const customRes = new CustomResponse(res);

  customRes.status(200).send('Hello,  Welcome to Itsourcecode!');
});

server.listen(3000, () => {
  console.log('Server listening on port 3000...');
});

5: Use http-status-codes module

Using the http-status-codes module you can able to get the “OK” status code and set it using the res.statusCode property.

The res.end() function sends the response body to the client.

const http = require('http');
const HttpStatus = require('http-status-codes');

const server = http.createServer((req, res) => {
  res.statusCode = HttpStatus.OK;
  res.end('Hello, Welcome to Itsourcecode!');
});

server.listen(3000, () => {
  console.log('Server listening on port 3000...');
});

Additional solutions for “typeerror: res.status is not a function”

If you still get the error after trying out all the solutions above, you can do the following:

✅Use the latest version of Express

This method was introduced in Express 4.0.0, so if you are using an older version, you have to upgrade.

Upgrade to the latest version of Express so that the “res.status” method is available.

✅Check for typos

You have to ensure that you haven’t misspelled the “status” method.

✅Check for conflicts

You have to remove any recently added modules or middleware to see if this resolves the issue.


This is because it is possible that another module or middleware is conflicting with the “res” object and causing the “status” method to be undefined.

✅Check your code

Verify your code to ensure that you are using the “res” object correctly.


The “res” object is typically passed as a parameter to a callback function that handles an HTTP request.

You should be using the “status” method to set the HTTP status code of the response.

Conclusion

By executing the provided solutions above, you can fix the typeerror: res.status is not a function error message.

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.

Thank you very much for reading to the end of this article.