npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@istanbul/http

v0.0.24

Published

Http packages of istanbul framework

Downloads

34

Readme

!! Not ready for production, experimental

What Is It?

This package is the main http package of the istanbul framework.

Features:

  • HTTP/1.1, HTTP/1.2 and HTTPS (all versions NodeJS supports nowadays) are supported by this package.
  • It hs its own router and middleware structure.
  • It contains many built-in supported functions (res.success, res.notFound, req.ip etc.)
  • Extends NodeJS's http types, so they are overwritable.
  • It has its own (overwritable) notFound and errorHandler mechanism.
  • It uses the latest JavaScript syntax and compiles them with TypeScript, you can write in any language (TypeScript, Module JavaScript or Common JavaScript) without worrying about version compatibility.
  • It's writable the way you're used to from Fastify and Express.
  • Fully tested, reliable.

Installation

Note: This package is 1st degree dependent on istanbul to work. Please take a look at @istanbul/app first if you haven't.

npm install @istanbul/http

or with yarn

yarn add @istanbul/http

Basic Usage

import {createApp, App} from '@istanbul/app';
import {createHttpServer, createRouter, createRoute, Request, Response, NextFunction} from "@istanbul/http";

const app = createApp<App>();
const server = createHttpServer();

const router = createRouter({prefix: 'api'});
router.get('somePath', (req: Request, res: Response, next: NextFunction) => {
  res.success('Hello World!');
});

app.register(server.build());
app.start();

Usage With Middleware

Middlewares can be used in 3 different ways.

In the route scope.

import {createApp} from "@istanbul/app";
import {createHttpServer, createRouter, createRoute, Request, Response, NextFunction} from "@istanbul/http";

const app = createApp<App>();
const server = createHttpServer();
app.register(server.build());

const router = createRouter({prefix: 'product'});

const checkProductId = (req: Request, res: Response, next: NextFunction) => {
  if (!!req.params.productId) return next();
  res.notFound("Product ID is required");
}

router.get(":productId", checkProductId, (req: Request, res: Response, next: NextFunction) => {
  res.success("Product found");
});

app.start();

In the router scope.

import {createApp} from "@istanbul/app";
import {createHttpServer, createRouter, createRoute, Request, Response, NextFunction} from "@istanbul/http";

const app = createApp<App>();
const server = createHttpServer();
app.register(server.build());

const router = createRouter({prefix: 'product'});

const checkProductId = (req: Request, res: Response, next: NextFunction) => {
  if (!!req.params.productId) return next();
  res.notFound("Product ID is required");
}

router.get(":productId", (req: Request, res: Response, next: NextFunction) => {
  res.success("Product found");
});

router.use(checkProductId);

app.start();

In the global scope.

import {createApp} from "@istanbul/app";
import {createHttpServer, createRouter, createRoute, Request, Response, NextFunction} from "@istanbul/http";

const app = createApp<App>();
const server = createHttpServer();
app.register(server.build());

const router = createRouter({ prefix: 'product' });

const checkProductId = (req: Request, res: Response, next: NextFunction) => {
  if (!!req.params.productId) return next();
  res.notFound("Product ID is required");
}

// http://localhost:3000/product/123
router.get(":productId", (req: Request, res: Response, next: NextFunction) => {
  res.success("Product found");
});

server.use(checkProductId);

app.start();

Use Versioned Routes

import {createApp} from "@istanbul/app";
import {createHttpServer, createRouter, createRoute, Request, Response, NextFunction} from "@istanbul/http";

const app = createApp<App>();
const server = createHttpServer();
app.register(server.build());

const router = createRouter({ prefix: 'product', version: 2 });

const checkProductId = (req: Request, res: Response, next: NextFunction) => {
  if (!!req.params.productId) return next();
  res.notFound("Product ID is required");
}

// http://localhost:3000/product/v2/123
router.get(":productId", (req: Request, res: Response, next: NextFunction) => {
  res.success("Product found");
});

server.use(checkProductId);

app.start();

Handle Request Ip Address

This package adds the request's ip address to the request object by default.

const handler = (req, res, next) => {
    res.success('Your ip: ' + req.ip);
}

Handle Request Body

This package parses JSON and Url-Encoded the request body by default.

const handler = (req, res, next) => {
    res.success(req.body);
}

Handle Request Query

This package parse the request query by default.

const handler = (req, res, next) => {
    res.success(req.query);
}

Handle Request Params

This package parse the request params by default.

router.post(":id", (req, res, next) => {
    res.success(req.params); // { id: '123' }
})

Handle Request Headers

You can select request headers in a friendly way through this package.

router.get("header", (req, res, next) => {
    const token = req.headers.get("token");
    // do something with token
})

router.post("header", (req, res, next) => {
    const token = "" // some encrypt process
    req.headers.set("token", token);
})

Handle Request Cookies

You can select request cookies in a friendly way through this package. All cookie options are available. And by default httpOnly is true. Everything is for your safety!

Options

export type CookieOptions = {
    httpOnly?: boolean; // default true
    secure?: boolean;
    maxAge?: number;
    path?: string;
    sameSite?: "strict" | "lax";
    domain?: string;
};

Examples

router.get("cookie", (req, res, next) => {
    const token = req.cookies.get("token");
    // do something with token
})

// not optioned
router.post("cookies", (req, res, next) => {
    const token = "" // some encrypt process
    req.cookies.set("token", token);
})

// with options
router.post("cookies", (req, res, next) => {
    const token = "" // some encrypt process
    req.cookies.set("token", token, {
        httpOnly: false,
        secure: true,
        maxAge: 1000 * 60 * 60 * 24 * 7,
        domain: "localhost",
        path: "/",
        sameSite: "strict"
    });
})

Options

This package has some crushable settings

Change Port

This package uses port 3000 by default. You can crush it.

server.config.port = 8080;
app.start();

Change Host

This package uses host 127.0.0.1 by default. You can crush it.

server.config.host = "localhost";
app.start();

Change Global Prefix

This package has its own main router. And this router uses the / prefix by default. All routes in your app depend on this prefix and you can override it if you want.

server.config.globalPrefix = "/api";
app.start();

Change Not Found Handler

This package has a notFoundHandler that returns results as follows by default. You can change this.

result:

{
    "success": false,
    "message": "Cannot GET /api/v2/products",
    "code": 404
}

change it:

server.config.notFoundRoute = (req, res, next) => {
    res.notFound("Not Found");
}
app.start();

Change Error Handler

This package handles errors by default and returns a result like the one below. You can change this.

result:

{
    "success": false,
    "message": "productId required",
    "code": 400
}

change it:

server.config.errorHandler = (err, req, res, next) => {
    res.error('Error: ' + err.message);
}
app.start();