Skip to content

Express style router for the Rapport request/response library

License

Notifications You must be signed in to change notification settings

miratronix/rapport-router

Repository files navigation

rapport-router CircleCI Coverage Status

NPM

Express style router plugin for Rapport, intended for use with rapport-http.

Installation

Node: Install the plugin via NPM: npm install --save rapport-router

Browser: Attach rapport.router.min.js to your HTML page

Then add the plugin to rapport:

// Globally
Rapport.use(require('rapport-router')); // In Node.js
Rapport.use(RapportRouter); // In the browser

// Or to a instance
Rapport(wsImplementation).use(require('rapport-router')); // In Node.js
Rapport(wsImplementation).use(RapportRouter); // In the browser

Usage

This plugin adds a method to the global Rapport constructor. To create a new router, simply call it:

const router = Rapport.Router();

Once you have a router, usage is similar to express.js:

// Add a get route for /users
router.get('/users', (req, res, next) => {
    res.status(200).send({
        users: []
    });
});

// You can nest routers
const userRouter = Rapport.Router();
const mainRouter = Rapport.Router();
userRouter.get('/users', () => {});
mainRouter.use('/v1', userRouter); // The exposed route is now /v1/users

// Or add middleware to all routes
router.use((req, res, next) => {
    console.log('Got a request'); 
});

// Regex is supported
router.get('/user/:id', (req, res, next) => {
    console.log(req.params.id);
});

// Route arrays and multiple handlers are also supported
router.get(['/users', '/people'], () => {}, () => {}); // For routes
router.use(() => {}, () => {}); // For middleware
router.use(['/v1', '/v2'], router1, router2); // And for routers

// Handler arrays are flattened recursively. Thus, the following two lines are equivalent
router.use('/v1', [() => {}, () => {}], () => {});
router.use('/v1', () => {}, () => {}, () => {});

// Returning a promise calls next automatically when the promise resolves
router.get('/user', 
    (req, res) => {
        return Promise.resolve();
    },
    (req, res) => {
        res.status(200).send({
            users: []
        });
    });

// You can add also add a error handler to all routes
router.error((err, req, res, next) => {
    
    // Explicitly try-catch when you send the response in your final error handler, to handle cases when the websocket 
    // is closed while handling the request.
    try {
        res.status(500).send('Something went wrong :(');
    } catch (err) {
        console.log('Failed to send the error response:', err);
    }
});

Once a router has been created, you can add it to the Rapport websocket:

// To just the specific websocket
Rapport(wsImplementation).create('url', { router: router }); // Or
Rapport(wsImplementation).wrap(existingSocket, { router: router });

// Or to all the sockets created by the Rapport instance
Rapport(wsImplementation, { router: router }); // Or
Rapport(wsImplementation).configure({ router: router });

// Or to all sockets created by all Rapport instances
Rapport.configure({ router: router });

About

Express style router for the Rapport request/response library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published