GithubHelp home page GithubHelp logo

base's Introduction

@synvox/base

A small Node.js router with middleware inspired by React hooks

npm i @synvox/base

Features

  • express style routing
  • composable sub routers
  • lightweight
  • getters that resemble react hooks
  • api inspired by zeit/micro

Basic Example

import http from 'http';
import { base, Router, getJson } from '@synvox/base';

const app = Router();

let name = 'World';

app.get('/', () => {
  return `Hello ${name}`;
});

app.post('/', async req => {
  const body = await getJson(req);
  name = body.name;
  return 'Ok';
});

http.createServer(base(app)).listen(3000);

Getters instead of middleware

With base you can create your own getters that replace the need for most middleware.

import { base, Router, getJson } from '@synvox/base';
import { connect, createError } from '@synvox/sql';

const sql = connect();

export const getUser = createGetter(async req => {
  const user = await sql`
    select * from users
    join user_tokens on users.id = user_tokens.user_id
    where user_tokens.id=${req.headers.token}
  `;

  if (!user) throw createError(401);

  return user;
});

export const isAdmin = createGetter(async req => {
  return await getUser(req).isAdmin;
});

// elsewhere
import { createError } from '@synvox/base';
import { getUser } from './getters';

app.get('/whoami', async req => {
  return await getUser(req);
});

app.get('/admin', async req => {
  const user = await getUser(req);
  const isAdmin = await isAdmin(req);
  if (!isAdmin) throw createError(401);

  // etc...
});

getters that are created with createGetter are memoized by req so in the previous example, calling getUser multiple times results in a single query to the database.

Responding to requests

Returning JSON

Returning JavaScript objects will result in them being sent as JSON.

reply

Return reply(body, [status], [headers]) to send a response with a status code or headers.

createError

Throw createError(status, [message]) to send a server error.

Built in getters

getText

Get the body of a request as plaintext.

getJson

Get the body of a request as JSON.

getQueryParams

Get the query parameters of a request as a JavaScript object.

getUrlParams

Get the url parameters of a request as a JavaScript object.

Routing examples

import { Router } from '@synvox/base';
const app = Router();

app.get('/', () => {
  return 'GET!';
});

app.post('/echo', async req => {
  const body = await getText(req);
  return body;
});

const subApp = Router();

subApp.get('/');

app.use('/sub', subApp); // now all subApp endpoints are prefixed with /sub

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.