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

@srvem/app

v0.5.2

Published

Srvem (pronounced as "serve 'em") is a super-fast and minimalist asynchronous middleware-oriented TypeScript server framework for Node.js. This is the core package of Srvem (contains a class used to construct a Srvem app).

Downloads

5

Readme

@srvem/app

Srvem (pronounced as "serve 'em") is a super-fast and minimalist asynchronous middleware-oriented TypeScript server framework for Node.js.
This is the core package of Srvem (contains a class used to construct a Srvem app).

Installation

npm install --save @srvem/app

Example

import { Srvem } from '@srvem/app'

// create a Srvem app
const app = new Srvem()

// add middleware like:
app.use(new SrvStatic('public'))

// add your custom handlers like:
app.handle(async (ctx: Context): Promise<void> => {
  // greetings?
  if (ctx.method === 'GET' && ctx.url === '/greet') {
    ctx.statusCode = 200
    ctx.body = 'Hello, world!'
  }
})

// listen on port 3000
app.server.listen(3000)

API

import { IncomingMessage, RequestOptions, Server, ServerResponse, ServerResponseHeaders } http from 'http'



/**
 * Used to create a Srvem app.
 */
declare class Srvem {
  /**
   * The Server.
   */
  readonly server: Server

  /**
   * Constructs a new Srvem application.
   */
  constructor()

  /**
   * Adds middleware to be executed, with order (including handlers),
   * whenever the Srvem server receives a request.
   *
   * @param middleware Srvem middleware
   */
  use(...middleware: MiddlewareBlueprint[]): void
  
  /**
   * Adds request handler callback function(s), set to be executed, with order
   * (including middleware), whenever the Srvem server receives a request.
   *
   * @param handlers Callback function that handles requests like a middleware
   */
  handle(...handlers: ((ctx: Context) => Promise<void>)[]): void
}



/**
 * A context created when a request is received by the server while listening.
 * All things related to the request and response are found in `this`.
 * The native http.Server request can be found at `this.request`.
 * Middlewares and handlers modify this (especially `this.statusCode` and `this.body`).
 */
declare class Context {
  /**
   * http.Server's request
   */
  request: IncomingMessage

  /**
   * The Date this context was created on.
   */
  readonly createdOn: number;

  /**
   * Response body.
   */
  body: Buffer | string | any

  /**
   * Request method.
   */
  readonly method: string

  /**
   * Request url.
   */
  readonly url: string

  /**
   * Construct a new Context from the native request and response objects.
   *
   * @param request http.Server's request
   * @param response http.Server's response
   */
  constructor(request: IncomingMessage, response: ServerResponse)

  /**
   * Response status code.
   */
  statusCode: number

  /**
   * Response status message.
   */
  statusMessage: string

  /**
   * Send date on response?
   */
  sendDate: boolean

  /**
   * Get response headers.
   */
  readonly headers: ServerResponseHeaders

  /**
   * Returns all response header names.
   */
  readonly headerNames: string[]

  /**
   * Checks if a response header with a provided name exists
   *
   * @param name Response header name
   */
  hasHeader(name: string): boolean

  /**
   * Gets the value of response header name (returns null if it doesn't exist).
   *
   * @param name Response header name
   */
  getHeader(name: string): number | string | string[]

  /**
   * Sets a value for a response header name.
   *
   * @param name Response header name
   * @param value Response header value
   */
  setHeader(name: string, value: string | string[]): void

  /**
   * Deletes a response header.
   *
   * @param name Response header name
   */
  removeHeader(name: string): void

  /**
   * Sets timeout on the response.
   *
   * @param milliseconds Timeout milliseconds
   */
  setTimeout(milliseconds: number): Promise<Context>

  /**
   * Finishes the response by requesting and receiving the response from a new path.
   *
   * @beta This feature has not been properly tested yet.
   *
   * @param path New path
   * @param statusCode Response status code
   * @param requestOptions Redirected request options
   */
  redirect(path: string, statusCode?: number, requestOptions?: RequestOptions): Promise<Context>

  /**
   * Is the response finished?
   */
  readonly finished: boolean

  /**
   * Ends the response after writing the response body.
   *
   * @param body Overriding response body
   * @param statusCode Overriding status code
   */
  finish(body?: any, statusCode?: number): Promise<Context>

  /**
   * Ends the response without writing the response body.
   *
   * @param statusCode Overriding status code
   */
  terminate(statusCode?: number): Promise<Context>
}



/**
 * An abstarct super class Srvem middleware inherit.
 */
declare abstract class MiddlewareBlueprint {
  /**
   * Where execution of the middleware begins.
   *
   * @param ctx The Context
   */
  abstract main(ctx: Context): Promise<void>
}



/**
 * Promise resolve type shortcut.
 */
declare type PromiseResolveType<T> = (value?: T | PromiseLike<T>) => void

/**
 * Promise reject type shortcut.
 */
declare type PromiseRejectType = (reason?: any) => void

See Also

  • @srvem/static - A Srvem middleware used to serve static files from a specified directory.
  • @srvem/router - A Srvem middleware used to develop routers and server APIs with asynchronous request handlers.

Credits

Kaleab S. Melkie <[email protected]>

License

MIT License
Copyright © 2017 srvem

Made with ❤ in Addis Ababa.