Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-assert

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-assert

assert with status codes

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.3M
decreased by-9.98%
Maintainers
1
Weekly downloads
 
Created

What is http-assert?

The http-assert npm package is a simple assertion library for HTTP server testing. It allows developers to assert certain conditions and automatically throw HTTP errors when those conditions are not met. This can help to streamline error handling in HTTP applications by providing a declarative way to validate requests and ensure that the server responds with the correct status codes and messages when something goes wrong.

What are http-assert's main functionalities?

Asserting conditions with HTTP error codes

This feature allows you to assert a condition and throw an HTTP error with a specific status code and message if the condition is false. In the code sample, if the user is not found, a 404 error with the message 'User not found' is thrown.

const assert = require('http-assert');

// Example usage in an Express route handler
app.get('/user/:id', (req, res, next) => {
  const user = getUserById(req.params.id);
  assert(user, 404, 'User not found');
  res.send(user);
});

Custom error properties

This feature allows you to add custom properties to the error object. In the code sample, if authentication fails, a 401 error is thrown with additional information about the user that attempted to log in.

const assert = require('http-assert');

// Example usage with custom error properties
app.post('/login', (req, res, next) => {
  const user = authenticate(req.body.username, req.body.password);
  assert(user, 401, 'Authentication failed', { user: req.body.username });
  res.send('Logged in successfully');
});

Other packages similar to http-assert

FAQs

Package last updated on 21 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc