1.4.10 • Published 2 years ago

express-addons v1.4.10

Weekly downloads
9
License
BSD-3-Clause
Repository
github
Last release
2 years ago

express-addons

Introduction

This is a small package for doing some addons on express.js 4.

Async Routes

Allows you to use async routes instead of callbacks.

Here is an example.

    route.get('/', async (req, res) => {
        
        if (!req.notAuthed) throw new Error('Not authorized.');
        
        await something();

        return 'This is my response.';
        
    });

It's simple and it's strait forward. Remark that the next parameter is not exposed to async routes.

Non-async routesAsync routes
next()Exit scope
next(new Error())throw new Error()
send(data)return data

You can configure which response method should be used when you return data from the route to be send - see below.

Configuration

    const { asyncRoutes } = require('express-addons');
    
    asyncRoutes(/* { options } */);

Options

These options are available for the asyncRoutes addon.

NameTypeDescriptionDefault
autosenderStringThe method on response to use when returning data from a route - e.g. send, json.send
- or -FunctionA custom function to use (has prototype (data, req, res)).

Catch all

Catch all is a simple route, that you put at the end of your other routes to handle 405 Method Not Allowed.

Here is an example.

    route.get('/mypath/', ...);
    route.post('/mypath/', ...);
    route.delete('/mypath/', ...);
    route.catchAll('/mypath/');

In the above example the catchAll route returns 405 Method Not Found to the client and sets the Allow header.

You can also provide an additional routes for custom response handling.

    route.catchAll('/mypath/', (req, res) => {
        res.json({ error: 'method not allowed');
    });

Remark the first parameter is a path, which can be omitted and defaults to /.

Configuration

    const { catchAll } = require('express-addons');
    
    catchAll();

You can provide an overall additional route, which will be used on all instances where route.catchAll() is used by providing it with the configuration.

    const { catchAll } = require('express-addons');
    
    catchAll((req, res) => {
        res.json({ error: 'method not allowed' });
    });

If you also configured the async routes addon, you can also use async routes in the custom catchAll routes.

License

New BSD License. See file LICENSE.

1.4.9

2 years ago

1.4.10

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago