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

lightserve

v0.1.6

Published

Lightweight NodeJS web server

Downloads

10

Readme

LightServe

LightServe is a little webserver giving everything you need.

Installation

npm install lightserve

OR

yarn add lightserve

Usage

import { App } from 'lightserve'

const app = new App()
app.addRoute({
  name : 'home',
  method : 'get',
  path : '/',
  handler : async (req, res) => {
    res.format(null, 'Hello World!')
  },
}).listen(3000)

API Reference

App

The App object is the global object that represents your webserver.

addRoute(options): App

Adds a route definition.

Arguments

| Name | Description | | ---- | ----------- | | options | Object defining a route | | options.method | Optional, Default get Method used for this route | | options.path | Optional Path of the route | | options.handler | Required Async Function taking req and res parameters | | options.authenticated | Optional Set this route as requiring authentication | | options.name | Name of the route |

use(middleware): App

Adds a middleware.

Arguments

| Name | Description | | ---- | ----------- | | middleware | Middleware definition | | middleware.before | Optional Sets the middleware to be executed before the route handler | | middleware.after | Optional Sets the middleware to be executed after the route handler | | middleware.fn | Required Middleware function, taking req and res parameters |

addAuthenticator(authenticator): App

Adds an authenticator handler. It's only meant to check if the incoming request matches your needs about the authentication.

Arguments

| Name | Description | | ---- | ----------- | | authenticator | Authenticator definition | | authenticator.authenticate | Required Function taking req as parameter |

cors(options): App

Enables CORS (Cross-Origin Resource Sharing) for your webserver.

Arguments

| Name | Description | | ---- | ----------- | | origin | Required You should always set the origin of your requests. Set it as * to set uncontrolled origin. | | allowMethods | Optional Set a list of allowed methods. It defaults to GET and POST | | credentials | Optional Sets if the cookies are meant to be received through the requests. Default to false | | allowHeaders | Optional Sets a list of allowed headers. It defaults to Accept,Content-Type,Content-Length,User-Agent | | exposeHeaders | Optional Sets a list of headers that are exposed by the server. Defaults to an empty string |

error(handler): App

Defines a global error handler for your webserver.

Arguments

| Name | Description | | ---- | ----------- | | handler | Function to be called whenever an error occurs in the process. It takes err, req and res arguments |

listen(port): App

Starts the server on the given port.

Arguments

| Name | Description | | ---- | ----------- | | port | Port to start the server on. |

Requests

LightServe adds some information inside the request object.

pathname: string

This is the actual url pathname without the query parameters.

query: object

The query object contains all the query parameters as an object.

body: object

The body object contains all the request body (for all requests but multipart ones where it is undefined).

multipart: object

The multipart object contains the request multipart body.

Each key of the object is a field name, each value is an object with a type property which value can be field or file.

For fields : The value is the string representation of what has been sent.

For files : The value is a buffer containing the file data.

context: object

The context is a flexible object you can use to store what you want.

I suggest you use that one to store some user authentication data.

cookies: object

List of cookies as an object where the keys are the cookie names, and values are the cookie values.

cors: object

The cors object contains the CORS configuration you set in the app.cors function.

Responses

LightServe adds some information inside the response object.

cookie(cookies: Array)

Sets a cookie in the response.

Arguments

| Name | Description | | ---- | ----------- | | cookies | Array of cookies items | | cookie.name | Required Name of the cookie | | cookie.content | Required Value of the cookie | | cookie.domain | Optional Domain set for the cookie validity | | cookie.path | Optional Path set for the cookie validity | | cookie.secure | Optional Flag set to restrict the cookie to HTTPS connections | | cookie.httpOnly | Optional Forces the cookie to not be accessible via JavaScript | | cookie.sameSite | Optional Sets the Same-Site policy for the browser (more information here https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Set-Cookie/SameSite) | cookie.expires | Optional Sets a JavaScript Date as the cookie expiration date | | cookie.maxAge | Optional Seconds the cookie will be accepted before expiration |

format(type: string, data: any, headers: object, status: number = 200)

Formats and sends a response.

Arguments

| Name | Description | | ---- | ----------- | | type | Type of the response, can be json or urlencoded | | data | Data the response will send | | headers | Additional headers the response will send | | status | HTTP status of the response |

error(data: any, status: number = 500)

Helper sending a JSON formatted error.

Arguments

| Name | Description | | ---- | ----------- | | data | Error data | | status | HTTP status of the error response |

Note that this helper will force the application/json content type for the response.