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

@techjs/create-http

v1.1.1

Published

A utility for setting up [@techjs/http](https://www.npmjs.com/package/@techjs/http) projects.

Downloads

10

Readme

TechJS | create-http

A utility for setting up @techjs/http projects.

Usage

npm init @techjs/http <dir>

Table of Contents

Installation

Run the @techjs/http generator to get started.

npm init @techjs/http <dir>

System Dependencies

About

The TechJS framework aims to provide a structured input event listener pattern by enforcing the principle of separation of concerns. The result is a clearly defined routes, controllers, and services. Through the power of object oriented programming, the @techjs/core library can be extended to provide a familiar interface between HTTP events and other application-layer input protocols, like event bus messages.

Warning

This example requires a bash environment. Using Bash on Windows or a console emulator like cmder should be fine. Alternatively, you can edit the npm scripts in the package.json file to work in your environment.

Developing

You will likely want more then just a ping server. Let's add a hashing service together to familiarize ourselves with creating new controllers, services, and routes. Once we are done, we will have a new route that will accept a url encoded string to hash given an algorithm specified in the URL path.

Adding a new service

// src/services/hash-service.ts

import * as crypto from 'crypto';

export class HashService {
  public async hash(algo: string, data: string | Buffer): Promise<string> {
    return new Promise<string>(
      (resolve, reject) => {
        const hash = crypto.createHash(algo);
        hash.on('readable', () => {
          const data: Buffer = <Buffer>hash.read();
          if (data) {
            resolve(data.toString('hex'));
          }
        });
        hash.on('error', (error) => {
          reject(error);
        });
        hash.write(data);
        hash.end();
      },
    );
  }
}

Note

We will want to export this serice from the src/services/ directory by adding an export statement in the src/services/index.ts file.

Adding a new controller

// src/controllers/hash.controller.ts

import { HttpController } from '@techjs/http';
import { Resolve } from 'tsnode-di';
import { HashService } from '../services';

export class HashController extends HttpController {
  @Resolve(HashService)
  private hash_service!: HashService;
  public async hash(): Promise<void> {
    try {
      const algo: string = this.req.params.algo;
      const data: string = this.req.query.data;
      const response: string = await this.hash_service.hash(algo, data);
      this.res.send(response);
    } catch (e) {
      this.res.sendStatus(500);
    }
  }
}

Note

We will want to export this controller from the src/controllers/ directory by adding an export statement in the src/controllers/index.ts file.

Registering the controller with the routes

// src/routes.ts

import { HttpRoute } from '@techjs/http';

// controllers

import {
  HashController,
  PingController,
} from './controllers/';

// routes

export const routes: Array<HttpRoute<any>> = [
  new HttpRoute('/ping', 'get', PingController, 'ping'),
  new HttpRoute('/hash/:algo', 'get', HashController, 'hash'),
];

Accessing our new route

We are now ready to run our server. We should have exposed a route at /hash/:algo. The HashController will hash any string passed to the data query parameter. Let's see what some requests look like

$ curl -G -X GET http://localhost:3000/hash/md5 --data-urlencod 'data=hello world'
5eb63bbbe01eeed093cb22bb8f5acdc3
$ curl -G -X GET http://localhost:3000/hash/sha256 --data-urlencod 'data=hello world'
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
$ curl -G -X GET http://localhost:3000/hash/foo --data-urlencod 'data=hello world'
Internal Server Error