Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/varthanv/express-header-middleware

A middleware for express js to validate your headers and abort the request with response when the criteria for the headers is not satisifed
https://github.com/varthanv/express-header-middleware

backend expressjs middleware

Last synced: 16 days ago
JSON representation

A middleware for express js to validate your headers and abort the request with response when the criteria for the headers is not satisifed

Awesome Lists containing this project

README

        

# express-header-middleware

A middleware for express js to validate your headers and abort the request with response when the criteria for the headers is not satisifed

## Installation

```sh
npm i express-header-middleware --save
```

## Usage

```js

import express, { Express } from "express";

const app: Express = express();

app.use(
headerMiddleware({
headers: [
{
name: "foo",
errorCode:"FOO",
httpStatusCode:403, // optional, Default 400
regExp:new RegExp("\\w+") // optional

},

],
})
);

app.get("/", (req, res) => {
res.send("fooo");
return;
});

app.listen(3000, () => {
console.log(`⚡️[server]: Server is running at http://localhost:3000`);
});

```

## Empty Case

```json
{
"error": "foo is required",
"errorCode": "FOO"
}
```

## Regex pattern is not matched

```json
{
"error": "foo does'nt match the regex pattern /\\d+/",
"errorCode": "FOO"
}
```