Coder Social home page Coder Social logo

pathfinder's Introduction

Pathfinder

pathfinder is a lightweight, HTTP-based router, built with node.js in mind, but can be used in the browser as well, since it has no external dependencies.

Installation

npm install ndugger/pathfinder --save

Usage

Simply import it at the top of a file, and go to town!

import * as Pathfinder from 'pathfinder';

Basic Usage Example

Here's an example to give you an idea on how to use it with a node http server.

import http from 'http';
import url from 'url';

import * as Pathfinder from 'pathfinder';

const router = new Pathfinder.Router();

router.get('/', async request => {
    return 'Hello, World!';
});

router.get('/{ foo }', async (request, { foo }) => {
    return `Hello, ${ foo }!`;
});

http.createServer(async (request, response) => {
    const path = url.parse(request.url).pathname;
    const route = router.find(request.method, path);
    
    if (route) {
        response.end(await route.resolve(request, route.params));
    }
}).listen(8080);

Middleware

pathfinder also supports "middleware", in that you can pass in an array of async functions to be called before an action. Middleware functions should throw a useful value that you can use to send an error response to the client if it fails.

async function authenticate(request) {
    if (!authenticated) {
        throw new Error(401);
    }
}

router.delete('/{ foo }', [ authenticate ], async (request, { foo }) => {
    // if authenticate throws, this action will not be executed
});
if (route) try {
    const result = await route.resolve(request, route.params);

    response.end(result);
}
catch (error) {
    response.statusCode = error.message;
    response.end(http.STATUS_CODES[ error.message ]);
}

Available API

router.connect('/', async request => {
    // ...
});
router.delete('/', async request => {
    // ...
});
router.get('/', async request => {
    // ...
});
router.head('/', async request => {
    // ...
});
router.options('/', async request => {
    // ...
});
router.patch('/', async request => {
    // ...
});
router.post('/', async request => {
    // ...
});
router.put('/', async request => {
    // ...
});
router.trace('/', async request => {
    // ...
});
router.find('GET', '/');

pathfinder's People

Stargazers

RIJordan avatar

Watchers

James Cloos avatar Nick Dugger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.