How to use the StatusCodes function from http-status-codes

Find comprehensive JavaScript http-status-codes.StatusCodes code examples handpicked from public code repositorys.

http-status-codes.StatusCodes is an enum that contains a set of named constants representing HTTP status codes, allowing you to easily reference them in your Node.js application.

1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
}
else {
	const statusCode = response.status;
	gthat.log.debug(`Received response from: ${url} (status code: ${statusCode} - ${response.statusText})`);

	if (statusCode != HttpStatus.StatusCodes.OK) {
		gthis.airInfo = null;
		gthat.log.error(`Invalid HTTP status code (${statusCode} - ${response.statusText}). Getting device data failed!`);
		return;
	}
fork icon3
star icon7
watch icon2

+ 11 other calls in file

91
92
93
94
95
96
97
98
99
100
});
exports.sendResetPasswordTokenStatus = sendResetPasswordTokenStatus;
exports.registerUser = (0, express_async_handler_1.default)((request, response, next) => __awaiter(void 0, void 0, void 0, function* () {
    const { username, email, password } = request.body;
    if (!username || !email || !password) {
        return next(new error_response_1.ErrorResponse(`Some of the fields are missing, please try again`, http_status_codes_1.StatusCodes.BAD_REQUEST));
    }
    if (yield (0, exports.verifyUserExists)(email)) { // If the user already exists
        return response.status(http_status_codes_1.StatusCodes.BAD_REQUEST).json({ success: false, message: "Staff Already exists with that e-mail address" });
    }
fork icon0
star icon1
watch icon1

+ 399 other calls in file

How does http-status-codes.StatusCodes work?

http-status-codes.StatusCodes is an enum provided by the http-status-codes package that contains a set of named constants representing HTTP status codes.

When http-status-codes.StatusCodes is used, the named constants can be referenced by their name (e.g. OK) or their numeric value (e.g. 200) in your Node.js application.

This can be useful for sending HTTP responses with the correct status codes, or for checking the status code of an incoming HTTP request or response.

The http-status-codes package is commonly used in Node.js applications to simplify working with HTTP status codes and ensure that they are used consistently throughout the application.

362
363
364
365
366
367
368
369
370
371
    return response.status(http_status_codes_1.StatusCodes.OK).json({ success: true, message: "Shifts updated successfully" });
}));
exports.deleteUserByID = (0, express_async_handler_1.default)((request, response, next) => __awaiter(void 0, void 0, void 0, function* () {
    const id = request.params.id;
    if (!(0, mongoose_1.isValidObjectId)(id)) {
        return next(new error_response_1.ErrorResponse(`User ID is invalid. Please check your ID again`, http_status_codes_1.StatusCodes.BAD_REQUEST));
    }
    yield user_model_1.User.findByIdAndDelete(id);
    return response.status(http_status_codes_1.StatusCodes.NO_CONTENT).json({ success: true, message: "User deleted succesfully" });
}));
fork icon0
star icon1
watch icon1

+ 286 other calls in file

66
67
68
69
70
71
72
73
74
75
let { role_name, first_name, last_name, email, password, gender, location, age, city, state, } = req.body;
console.log(roleCheck());
let isAvailable = yield user_1.User.findOneBy({ email: email });
if (isAvailable) {
    return res.status(http_status_codes_1.StatusCodes.BAD_REQUEST).json({
        status: http_status_codes_1.StatusCodes.BAD_REQUEST,
        message: " User already exist",
    });
}
let timestamp = new Date();
fork icon0
star icon0
watch icon1

+ 10 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const http = require("http");
const StatusCodes = require("http-status-codes").StatusCodes;

const server = http.createServer((req, res) => {
  res.statusCode = StatusCodes.OK;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World!");
});

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

In this example, we're using the http module to create a new HTTP server. We're then using http-status-codes.StatusCodes to set the status code of the HTTP response to 200 OK. We're also setting the Content-Type header to text/plain and sending the response body "Hello World!". Finally, we're starting the server and listening for incoming requests on port 3000. When we run this code and access the server in a web browser or with a tool like curl, we'll receive an HTTP response with a status code of 200 OK and the message "Hello World!" in the response body. As you can see, http-status-codes.StatusCodes has made it easy to set the correct HTTP status code in our application without having to remember the numeric value of 200.

70
71
72
73
74
75
76
77
78
79
    });
}
catch (err) {
    if (err.code === 11000) {
        return res.status(409).json({
            status: http_status_codes_1.StatusCodes.CONFLICT,
            message: "Email already exist",
        });
    }
    next(err);
fork icon0
star icon0
watch icon1

+ 94 other calls in file