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

@alpinesoft/express-plus

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alpinesoft/express-plus

Lightweight Express.js middleware aimed to create standardised REST APIs

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ExpressPlus

This lightweight Express.js middleware focuses on two main features:

  • (Type-) checking the request body
  • Standardising the result/error format

Getting started

const expressPlus = require('express-plus')
app.use(expressPlus.createMiddleware())

Check request body

const { Rule } = require('@cesium133/forgjs')
const bodyRules = {
    username: new Rule({
      type: 'string',
      minLength: 3,
      maxLength: 120,
      notEmpty: true
  }),
  password: new Rule({
      type: 'password',
      minLength: 3,
      maxLength: 120,
      notEmpty: true
  })
}

app.post('/', (req, res) => {
    
    const isValid = req.checkBody(bodyRules)
    
    // Error will be sent automatically
    if (!isValid) {
        return
    }
    
    // Continue handling the request if body is valid
})

This will result in:

{
    "status": "ERROR",
    "error": {
        "name": "Invalid request body",
        "message": "Could not parse request body, check for invalid or missing fields"
    }
}

Return data if request is successful

app.post('/', (req, res) => {
    res.resolve({
        yourData: 'FOR-EXAMPLE-A-TOKEN'
    })
})

This will result in:

{
    "status": "SUCCESS",
    "payload": {
        "yourData": "FOR-EXAMPLE-A-TOKEN"
    }
}

Create custom errors

res.reject(expressPlus.createHttpError(500, 'Your error', 'Provide a concise error message.'))

or

next(expressPlus.createHttpError(500, 'Your error', 'Provide a concise error message.'))

Handle errors

app.use(expressPlus.createErrorHandler())

Place this middleware usage call after every other to ensure full error handling. To learn more about Express.js error handling follow this link.

You can see a full example here.

FAQs

Package last updated on 09 Jan 2019

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