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

serverette

v0.9.1

Published

Node.js Testing Web Server

Downloads

11

Readme

Serverette — Node.js Testing Web Server

This is a web server as test bed you don't need to install Apache, Express, or other web server to test your HTML/CSS web application front-end.

Note: This server is not for production. Serverette currently support only GET, HEAD, POST, PUT, DELETE, OPTIONS, and PATCH methods. CONNECT and TRACE are not supported.

Roadmap: Find reason to add CONNECT and TRACE methods.

Installation

npm install serverette --save-dev

ES Module VS CommonJS

To know if your project is ES Module type look at the package.json file, it should have "type": "module", line. Your package.json should look something similar to this:

{
  "name": "nodejs",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^18.0.6"
  },
  "devDependencies": {
    "serverette": "^0.9.0"
  }
}

Setup

Import into your application ES module and create __dirname:

// import Serverette
import url from "url";
import { serverette } from "serverette";

// get current directory
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

Import into your application in CommonJS (__dirname automatically available):

// import Serverette
const serverette = require("serverette")

You can import both Serverette and callbacks object (ES Module):

// import Serverette and callbacks
import { serverette, callbacks } from "serverette"

You can import both Serverette and callbacks object (CommonJS:

// import Serverette and callbacks
const { serverette, callbacks } = require("serverette")

Now you can create your web server with all defaults:

// create server "test" directory with test web files:
serverette(__dirname + "/test");

Replace /test with the name of the test directory. The __dirname will return the full path of your working directory.

Important: You need at least one index.htm file and must be with .htm extension, not .html.

Usage

// Serverette acceptable parameters, rootDir is always required
// callbacks is an object of these http types {get, post, put, options, patch}
serverette(rootDir, port, mime, callbacks)
  • rootDir (required): The full working directory can use the built-in __dirname variable and must add the directory of the tested files, for example: __dirname + "/test".
  • port (optional): The default is 8080. Can choose anything you want.
  • mime (optional): Built-in MIME according to mozilla.org. You can add your own MIME type if it is not there. MIME should be an array with objects of mime type and regex for the file extension. Example adding mp3 file type: serverette(__dirname+"/test", 8080, [{ regex: /\.mp3/, type: "audio/mp3" }])
  • callbacks {get, post, put, options, patch} (optional): these are optional custom functions if the default behavior of printing data to the console is not satisfying.

HTTP Types Callback Behavior

  • GET Callback (optional): If no callback function provided, it will print the URL parameters (search query) data in console (terminal). Note: parameters returned by Serverette are in array pairs like: [["key","value"],["key","value"],["key","value"]]
  • HEAD Callback (optional): It will print HEAD with empty array data in console. Note, HEAD do not accept body/content in request.
  • POST Callback (optional): If no callback function provided, it will print the POST request data in console.
  • PUT Callback (optional): If no callback function provided, it will print the PUT data in console.
  • DELETE Callback (optional): Like HEAD, DELETE do not accept body (content) in request, but it will print DELETE in console with empty array.
  • UPDATE Callback (optional): Similar to PUT, if no callback function provided, it will print the UPDATE data in console.

NOTE: For all callback above, you can use your custom callback function. Serverette will return an array of the streamed data as a parameter for your callback function like: yourCallbackFunction(["data stream 1","data stream 2"]) you may need your function to combine or iterate through them.

Understand Callback Functions

Serverette support callback function to catch the request body received and to hijack the response before sending it to browser/client. Your custom function/callback will get an array of the data stream as the first parameter and the response object as the second parameter. If you want to add custom POST behavior, you need to import callbacks object first and add your custom post function. Note that post function is in lowercase.

import { serverette, callbacks } from "serverette"

callbacks.post = function(array,response){

	// printing POST body to the console:
    console.log("log array of POST", array)

    // Write response (also can redirect if you want) and close connection
    response.writeHead(200, { 'Content-Type': 'text/html' })
    response.end("POST data received!")
}
serverette(__dirname + "/test",'','',callbacks);

Full Example

import url from "url";
import { serverette, callbacks } from "serverette"

// Get current dir
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

// add custom POST behavior:
callbacks.post = function(array,response){
    console.log("log array of POST", array)
    response.writeHead(200, { 'Content-Type': 'text/html' })
    response.end("POST data received!")
}

// add custom mime:
const myMp3Mime = [{ regex: /\.mp3/, type: "audio/mp3" }]

serverette(__dirname + "/test", 8080, myMp3Mime, callbacks);

Terminal output will be something similar to this:

Serverette port #: 8080
======================
GET: / MIME: text/html
GET: /w3e.css MIME: text/css
GET: /w3-theme-blue-grey.css MIME: text/css
GET: /h5p-font-awesome.min.css MIME: text/css
GET: /w3e.js MIME: text/javascript
GET: /snow.jpg MIME: image/jpeg
GET: /forest.jpg MIME: image/jpeg
GET: /car.jpg MIME: image/jpeg
GET: /mountains.jpg MIME: image/jpeg
GET: /lights.jpg MIME: image/jpeg
HEAD: []
DELETE: []
PUT: [ '{"test":"testing PUT method"}' ]
PATCH: [ '{"test":"testing PATCH method"}' ]
POST: [ '{"test":"testing POST method"}' ]
OPTIONS: [ '{"test":"testing OPTIONS method"}' ]
=============================
URL(GET) parameters found!
URLSearchParams { 'v' => '4.5.0' }
v 4.5.0
=============================
GET: /fontawesome-webfont.woff2 MIME: font/woff
GET: /fontawesome-webfont.woff2 MIME: font/woff2
GET: /favicon.ico MIME: image/x-icon
POST: [ 'name=Joe&email=Joe%40example.com&subject=Register+me&gender=male' ]

Support, Feedback & Issues

Your contribution is very important, please, visit this link: (https://github.com/emnawer/serverette/issues).