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

tamed-express-server

v3.0.2

Published

A quick express server that converts a js file to express routes, using the js file's exported functions.

Downloads

7

Readme

Why?

This library is implemented to convert a .js file quickly to an Express server, using the exported function names as routes. The rules to convert function names to rotes are:

  1. Function names of the form camelCase are converted to kebab-case).
  2. The underscore character _ is converted to a slash /.

It;

  • takes the file as a parameter,
  • analyze the exported keys (assuming all exports are functions or the special exportedForTesting object where its keys are also only functions),
  • exclude the reserved exports constructor, init, destroyer functions and exportedForTesting object
  • if there are whitelists for the functions, it will only serve the whitelisted functions (both the normal and the test ones)
  • and serves the remaining keys of the export as kebab-cased routes.
    • If the handler returns a payload response is served as application/json with the payload as the body.
    • If the handler returns a string response is served as text/plain with the string as the body.

The library assumes all the keys are functions that receive a single object (the body) and the response of those functions are directly passed to the response towards the client.

Installation

yarn add tamed-express-server

API

| Function | Description | | --- | --- | | expressApp | The main function that takes the file path, whitelist and testWhitelist as parameters. |

The parameters for the expressApp function:

| Parameter | Description | | --- | --- | | handlerFile | The full path of the file that will be served. | | functionsWhitelist | An array of strings that contains the names of the functions that will be served. | | testFunctionsWhitelist | An array of strings that contains the names of the functions that will be served from the exportedForTesting object. |

Example

// sample-handler.js
...
module.exports = {
	normalRoute, // to be exposed as /normal-route route
	shouldBeExcluded, // not to be exposed because of the whitelist
	init, // not to be exposed because of default exclusion criteria
	constructor, // not to be exposed because of default exclusion criteria
	destroyer, // not to be exposed because of default exclusion criteria
	exportedForTesting: {
		testRoute, // to be exposed as /test/test-route route
		shouldBeExcludedTest // this function should not be exposed because of the whitelist
	}
}
// server.js
const path = require('path');

const tes = require('tamed-express-server');

const startServer = async () => {
	// httpsKeys, p_port, handlerFile, functionsWhitelist, testFunctionsWhitelist
	let whitelist =['normalRoute'];
	let testWhitelist = ['testRoute'];
	let fileFullPath = path.join(__dirname, 'sample-handler.js');
	tes.expressServer(undefined, 3000, fileFullPath, whitelist, testWhitelist);
}

startServer();

License

The license is MIT and full text here.

Used Modules' Licenses

  • express license here.
  • path license here.
  • body-parser license here.
  • cors license here.
  • morgan license here.
  • tick-log license here.