Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lightserve

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lightserve

Lightweight NodeJS web server

  • 0.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

NameDescription
optionsObject defining a route
options.methodOptional, Default get Method used for this route
options.pathOptional Path of the route
options.handlerRequired Async Function taking req and res parameters
options.authenticatedOptional Set this route as requiring authentication
options.nameName of the route
use(middleware): App

Adds a middleware.

Arguments

NameDescription
middlewareMiddleware definition
middleware.beforeOptional Sets the middleware to be executed before the route handler
middleware.afterOptional Sets the middleware to be executed after the route handler
middleware.fnRequired 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

NameDescription
authenticatorAuthenticator definition
authenticator.authenticateRequired Function taking req as parameter
cors(options): App

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

Arguments

NameDescription
originRequired You should always set the origin of your requests. Set it as * to set uncontrolled origin.
allowMethodsOptional Set a list of allowed methods. It defaults to GET and POST
credentialsOptional Sets if the cookies are meant to be received through the requests. Default to false
allowHeadersOptional Sets a list of allowed headers. It defaults to Accept,Content-Type,Content-Length,User-Agent
exposeHeadersOptional 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

NameDescription
handlerFunction 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

NameDescription
portPort 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

NameDescription
cookiesArray of cookies items
cookie.nameRequired Name of the cookie
cookie.contentRequired Value of the cookie
cookie.domainOptional Domain set for the cookie validity
cookie.pathOptional Path set for the cookie validity
cookie.secureOptional Flag set to restrict the cookie to HTTPS connections
cookie.httpOnlyOptional Forces the cookie to not be accessible via JavaScript
cookie.sameSiteOptional Sets the Same-Site policy for the browser (more information here https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Set-Cookie/SameSite)
cookie.expiresOptional Sets a JavaScript Date as the cookie expiration date
cookie.maxAgeOptional Seconds the cookie will be accepted before expiration
format(type: string, data: any, headers: object, status: number = 200)

Formats and sends a response.

Arguments

NameDescription
typeType of the response, can be json or urlencoded
dataData the response will send
headersAdditional headers the response will send
statusHTTP status of the response
error(data: any, status: number = 500)

Helper sending a JSON formatted error.

Arguments

NameDescription
dataError data
statusHTTP status of the error response

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

FAQs

Package last updated on 14 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc