GithubHelp home page GithubHelp logo

borisovg / expressus Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 2.0 729 KB

A small, fast, Express-like Node.js web framework.

License: GNU Lesser General Public License v3.0

Makefile 2.35% TypeScript 97.65%

expressus's Introduction

Tests Coverage Status

expressus

A small, fast, Express-like Node.js web framework.

Installation

npm install --save @borisov/expressus

Usage Example

const { App, middleware } = require('@borisovg/expressus');
const { createServer } = require('http');

const app = new App();
const server = createServer(app.router);

server.listen(8080);

// simple GET route

app.get('/foo', function (req, res) {
    res.end('OK');
});

// GET route with params (e.g. /foo/bar/baz)

app.get('/foo/:a/:b', function (req, res) {
    console.log(req.params);
    res.end('OK');
});

// JSON POST route

app.use(middleware.json());

app.post('/foo', function (req, res) {
    console.log(req.body);
    res.json({ result: 'OK' });
});

TypeScript Example

When using any of the included middleware, or if you add custom middleware that modifies the request or response object passed to the handler you can pass additional type information to the generic App class.

import { App, middleware } from '@borisovg/expressus';
import type { JsonRequest, JsonResponse, QueryRequest } from '@borisovg/expressus';

const app = new App<JsonRequest & QueryRequest, JsonResponse>();
const server = createServer(app.router);

server.listen(8080);

app.use(middleware.json());
app.use(middleware.query());

app.get('/foo/:bar', function (req, res) {
    // req.body, req.params.bar, req.query and res.json will be typed
});

API

  • framework.App() - application constructor
  • app.get(route, callback) - register GET handler
  • app.delete(route, callback) - register DELETE handler
  • app.patch(route, callback) - register PATCH handler
  • app.post(route, callback) - register POST handler
  • app.put(route, callback) - register PUT handler
  • app.remove_all_handlers() - remove all handlers
  • app.remove_middleware(fn) - remove middleware function
  • app.router(req, res) - router function (use as request callback for HTTP server)
  • app.use(fn) - register middleware function

Routing

  • route "/foo" will match request path "/foo"
  • route "/foo/:name" will match request path like "/foo/bar" and set req.params.name to "bar"
  • route "/foo/:name/*" will match request path like "/foo/bar/anything/else", will set req.params.name to "bar" and set req.splat to "anything/else"
  • raw route string is used as req.route

Refer to the http-hash package for more information.

Middleware

A middleware function has the signature (req, res, next), with next being a function that will run the next middleware in the chain. Middleware functions are run in the order they were attached.

Some basic middleware is included in the framework.

Request Body Loader

This middleware will load the request body and attach it to req.body as a buffer.

app.use(middleware.body());

Request Body Form Parser

This middleware will parse req.body form data and replace req.body with the result.

app.use(middleware.form());

Request Body JSON Parser

This middleware will parse req.body JSON data and replace req.body with the result. It will also add a res.json(data) convenience method.

app.use(middleware.json());

Request Query String Parser

This middleware will load parse the request query string and attach it to req.query.

app.use(middleware.query());

Static Content Server

This middleware is a very simple static content server, aimed for use during development. In production, consider fronting your app with a real HTTP server (e.g. Nginx) for superior performance.

if (process.env.NODE_ENV !== 'production') {
    app.use(middleware.static({ path: './public' }));
}

expressus's People

Contributors

borisovg avatar

Stargazers

Aleksei Kozadaev avatar

Watchers

Aleksei Kozadaev avatar James Cloos avatar  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.