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

@zaeny/http

v1.0.23

Published

Utility function creating http server, Translate incomin http request into data strcuture like ring in clojure

Downloads

84

Readme

@zaeny/http

npm version npm downloads

Simple HTTP Server in node.js

Translate incomin http request into data strcuture like ring in clojure
Provide basic funcitonal programming utility creating http server

Getting started

npm i @zaeny/http

Usage

basic http server, with handler accept returning object {status, headers, body}


var { createServer, startServer, response } = require('@zaeny/http');

var mainHandler = (req, res) => ({ 
  status: 200, headers: {}, body: 'Hello world'
});

var server =  createServer({ 
  port: 8081, 
  handler: (req, res) => mainHandler(req, res)
});

startServer(server);

basic response with utility functions

var { createServer, startServer, response } = require('@zaeny/http');

var handler = (req, res) => response('hello world');
var server =  startServer(
  createServer({ port: 8081, handler: (req, res) => handler(req, res)})
);

// response json, if body isArray or isObject
var handler = (req, res) => {
  return {status: 200, headers: contentType('json'), body: { is_running: true }};
}

// use response
var handler = () => response({ is_running: true}, contentType('json'));

// contentType, findFile, status, headers, created, notFound, badRequest, etc..

create your own simple routing


var responseIndex = (req, res) => findFile('./index.html');
var getUser = (req, res) => response('fetch all  user');

var mainHandler = (req, res) => {
  let routes = {
    `GET /`: responseIndex,
    `GET /api/user`: getUser
  }
  let resolve = routes[`${req.method} ${req.path}`];
  if(resolve) return resolve(req, res);
  return notFound();
}

var server = server || startSever(
  createServer({ port: 8081, handler: (req, res) => mainHandler(req, res)})
);

usage with @zaeny/clojure.core

var {threadFirst, assoc} = require('@zaeny/clojure.core');

var info = (req, res) => response('hello world');

var server = threadFirst(
  { port: 8081, handler: info },
  [createServer],
  [startServer]
);

advanced routing with params :id with findRotues


var routes = () => ({
  'GET /api/audio/chapter/:id': 'handleGetAudioByChapter',
  'GET /asset/:id': 'serveAudioAssets'  
});

var resolve = {
  serveAudioAssets,
  handleGetAudioByChapter
}

var mainHandler = (req, res) => {
  let found = findRoutes(routes(), req);
  if(found && resolve[found]){
    return resolve[found](req, res);
  }
  return notFound('404');
}

var server = threadFirst(
  { port: 8081, handler: mainHandler },
  createServer,
  startServer
);

testing and debuging from repl, createRequest to test the handler



// create requst with headers 
createRequest('GET /api/users', {headers: {'Authorization': 'Basic aziz=pass'}});

await handler(createRequest('GET /api/search?query=Aziz'));

API

  createServer,
  startServer,
  stopServer,
  getServer,
  response,
  redirect,
  created,
  badRequest,
  notFound,
  status,
  header,
  headers,
  contentType,
  cors,
  isContentType,
  responseWrite,
  clientRequest,
  responseBuffer,
  responseWith,
  readFile,
  findFile,
  mimeType,
  findRoutes,
  createRequest,
  notModified,
  ext

Related work

  • Composable - Collection of functions to solve programming problem

Changes

  • [1.0.0] expose common api dealing with creating http server
  • [1.0.1] add findRoutes utility basic routing GET /api/audio/:id, params matching and stringify body request if json
  • [1.0.2] add support for async handler
  • [1.0.3] add support buffer non string request
  • [1.0.4] fix contentType should return not headers
  • [1.0.5] add createRequest to mockup request call
  • [1.0.6] add notModified and fix request parser
  • [1.0.8] add fix requst.body parse if empty dont parse
  • [1.0.9] add parsing request body, add improvement processing response
  • [1.0.10] add parsing body response if non array or object into string.
  • [1.0.11] add response(body, headers, status) basic construct instead single arguments, fix findRoutes bugs
  • [1.0.12] fix bugs request.buffer and request.body is empty when content-type application/json
  • [1.0.13] fix bugs replace not found on findRoutes
  • [1.0.15] fix homepage repository
  • [1.0.16] improve performace parseRequest, remove setTimeout, add chunk handling, better parseRequest
  • [1.0.17] fix buffer request, remove request method parser for non GET
  • [1.0.18] add clientError handler, improve parsing by urlCache set and get
  • [1.0.20] fix internal server error url cache not defined (bugs)
  • [1.0.23] update fix error bugs urlCache parse unable to parse