You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

express-res-status-handler

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-res-status-handler

Middleware for status handler


Version published
Weekly downloads
1
decreased by-96.15%
Maintainers
0
Created
Weekly downloads
 

Readme

Source

npm-res-status-handler

express-res-status-handler

express-res-status-handler is an npm package for handling various response statuses in an Express server with custom middleware.

Installation

npm i express-res-status-handler

Usage example

Import the package

import customResponsesMiddleware from 'express-res-status-handler';

Import the config file

import { myConfig } from './config/myconfig.js'; 

Use the middleware to configure

const resConfig = [myConfig] # Initialize your multiple config files
app.use(customResponsesMiddleware(resConfig)); 

Your config sample json files

Difine your multiple config files

const myConfig = {
    custom: {
        error: false,
        code: 200,
        message: "success",
        response: null,
        count: 0,
        pagination: {}
      }
  };
  
  export {myConfig}

Use your custom responses

# Example route using 'custom' configuration
app.get('/custom', (req, res) => {
  res.custom({ message: 'My custom response messages' });
});

Sample code:

import express from 'express';
import customResponsesMiddleware from 'express-res-status-handler';

import { myConfig } from './config/myconfig.js'; #import your custom config

const app = express();

# Use customResponsesMiddleware with multiple configuration files
app.use(customResponsesMiddleware([myConfig]));

# Example route using existing keys
app.get('/success', (req, res) => {
  res.success({ message: 'Config herer done' });
});

# Example route using 'custom' configuration
app.get('/custom', (req, res) => {
  res.custom({ message: 'Custom response message' });
});

#Run App
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Default Response Message


/ Example route using existing keys
app.get('/success', (req, res) => {
  res.success({ message: 'Config here done' });
});

app.get('/custom', (req, res) => {
  res.custom({ message: 'My custom response messages' });
});

app.get('/forbidden', (req, res) => {
  res.forbidden({ message: 'Access forbidden' });
});

app.get('/unauthorized', (req, res) => {
  res.unauthorized({ message: 'Unauthorized access' });
});

app.get('/not-found', (req, res) => {
  res.notFound({ message: 'Resource not found' });
});

app.get('/internal-error', (req, res) => {
  res.internalServerError({ message: 'Internal server error' });
});

app.get('/redirect', (req, res) => {
  res.redirect({ message: 'Redirecting', location: 'https://example.com' });
});

app.get('/timeout', (req, res) => {
  res.timeout({ message: 'Request timeout' });
});

Keywords

FAQs

Package last updated on 30 Jun 2024

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc