1.7.6 • Published 4 years ago

@fundaciobit/express-middleware v1.7.6

Weekly downloads
45
License
MIT
Repository
github
Last release
4 years ago

Miscellaneous Express middlewares

A miscellaneous collection of Express middlewares.

Note: For specific Express middleware wrappers for Redis and MongoDB, please visit @fundaciobit/express-redis-mongo

Middlewares

middlewaredescription
ipv4Extracts IP address and converts IPv6 format to IPv4.
parseTypesParses string properties into numbers or booleans.
validateJsonSchemaValidates an instance with a provided JSON Schema.
verifyJWTVerify a JSON Web Token.
signJWTSign a JSON Web Token.

Install

npm install @fundaciobit/express-middleware

Index

ipv4

Middleware to extract the IPv4 address from the request object (it converts IPv6 format to IPv4 format). The extracted address will be available on the request via the ipv4 property.

Usage

const express = require('express')
const { ipv4 } = require('@fundaciobit/express-middleware')

const app = express()

app.use(ipv4())

app.get('/ip', (req, res) => {
  const { ipv4 } = req
  res.json({ ipv4 })
})

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

const port = 3000
app.listen(port, () => { console.log(`Server running on port ${port}...`) })

parseTypes

Middleware to convert string properties of an object to numbers (integers or floats) or booleans. The provided object will not be mutated. The copied and parsed object will be available on the request via parsedObject property.

Parameters

  • objectToParse: (required) Function that accepts the request object as parameter, that returns the object to parse.
  • properties: (optional) Array of properties to parse. If not provided, the conversion is applied to all properties of the object.

Usage

const express = require('express')
const { parseTypes } = require('@fundaciobit/express-middleware')

const app = express()

app.get('/users/min_age/:min_age/max_age/:max_age/is_employee/:is_employee/min_salary/:min_salary/max_salary/:max_salary',
  parseTypes({
    objectToParse: (req) => req.params
  }),
  (req, res) => {
    const { params, parsedObject } = req
    res.status(200).json({ params, parsedObject })
  })

app.use((err, req, res, next) => {
  if (!err.statusCode) err.statusCode = 500
  res.status(err.statusCode).send(err.toString())
})

const port = 3000
app.listen(port, () => { console.log(`Server running on port ${port}...`) })

validateJsonSchema

Middleware to validate the structure of an instance with the provided JSON Schema.

Parameters

  • schema: (required) is a JSON Schema object.
  • instanceToValidate: (required) is a function that accepts the request object as parameter, that returns the 'instance' to validate (string, array or object).

Usage

const express = require('express')
const bodyParser = require('body-parser')
const { validateJsonSchema } = require('@fundaciobit/express-middleware')

const app = express()

app.use(bodyParser.json())

app.post('/login',
  validateJsonSchema({
    schema: {
      type: 'object',
      required: ['username', 'password'],
      properties: {
        username: { type: 'string' },
        password: { type: 'string' },
      },
      additionalProperties: false
    },
    instanceToValidate: (req) => req.body
  }),
  (req, res) => {
    res.sendStatus(200)
  })

app.use((err, req, res, next) => {
  if (!err.statusCode) err.statusCode = 500
  res.status(err.statusCode).send(err.toString())
})

const port = 3000
app.listen(port, () => { console.log(`Server running on port ${port}...`) })

verifyJWT

Middleware to verify a JSON Web Token. The token to verify is extracted from:

  • the Authorization header as a bearer token ( Authorization: Bearer AbCdEf123456 ),
  • or through a token query parameter ( http://...?token=AbCdEf123456 ).

If the token is verified, then the decoded token payload is available on the request via the tokenPayload property, and the control is passed to the next middleware.

Parameters

  • secret: (required) is a string, buffer, or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA, as described in jsonwebtoken.

Usage

const express = require('express')
const { verifyJWT } = require('@fundaciobit/express-middleware')

const app = express()

app.get('/protected',
  verifyJWT({ secret: 'my_secret' }),
  (req, res) => {
    res.sendStatus(200)
  })

app.use((err, req, res, next) => {
  if (!err.statusCode) err.statusCode = 500
  res.status(err.statusCode).send(err.toString())
})

const port = 3000
app.listen(port, () => { console.log(`Server running on port ${port}...`) })

signJWT

Middleware to sign a JSON Web Token. The signed token will be available on the request via token property.

Parameters

  • payload: (required) is a function that accepts the request object as parameter, that returns an object literal, buffer or string representing valid JSON.
  • secret: (required) is a string, buffer, or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA, as described in jsonwebtoken.
  • signOptions: (optional) is an object with extra info to encode, as described in jsonwebtoken. Eg: { expiresIn: '24h' }

Usage

const express = require('express')
const bodyParser = require('body-parser')
const { signJWT } = require('@fundaciobit/express-middleware')

const app = express()

app.use(bodyParser.json())

app.post('/login',
  // Here include a middleware to verify user credentials from req.body:
  //  If Ok: set user info in req.user (without password) and call next().
  //  Else: call next(error) to handle the authentication error.
  signJWT({
    payload: (req) => ({
      username:  req.user.username,
      role: req.user.role
    }),
    secret: 'my_secret',
    signOptions: { expiresIn: '24h' }
  }),
  (req, res) => {
    const { token } = req
    res.status(200).json({ token })
  })

app.use((err, req, res, next) => {
  if (!err.statusCode) err.statusCode = 500
  res.status(err.statusCode).send(err.toString())
})

const port = 3000
app.listen(port, () => { console.log(`Server running on port ${port}...`) })
1.7.6

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.9

4 years ago

1.6.8

4 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.4

4 years ago

1.6.5

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.1.2

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago