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

yttrium-server

v1.0.3

Published

The jQuery web server framework

Downloads

9

Readme

:skull: (Do not use in production. Maybe.) :skull:

Installation

npm install yttrium-server --save

Some Benchmarks

Running autocannon -c 100 -d 5 -p 10 http://localhost:8000 with a JSON response:

Platform | req/sec ---------|-------- Koa | 23,028 Yttrium | 21,259 Express | 13,370 Hapi | 3,371

How to Use

Have a look at the example to see how easy it is get a fully functional Yttrium server up. Below is a brief outline of some of the core functionality.

Getting Started

The most basic Yttrium server:

const Y = require('yttrium-server');

// instantiating the Yttrium jQuery instance
// and the HTTP server object
const { $, server } = Y();

// on any HTTP request, send Hello World
$(server).on('request', (server, req, res) => {
    res.end('Hello World');
});

// start up server listener
$(server).listen(8000);

$(server).on('listening', (e) => {
  console.log('Server is listening on port:', e.target.address().port);
});

Routing

Add endpoints with the $.route('<endpoint>') function -- with the brackets. The Yttrium router utilizes a Router DOM, where each endpoint is an HTML element. The logic that fires upon request to those endpoints is defined by the on('route') event handler. The Router DOM has an <index> route built-in, which corresponds to the / endpoint. Further endpoints should be appended to the index route.

By default all methods are accepted and bodies and query strings are parsed. If you want to specify a method, use the data-method="method"attribute on the endpoint element.

Route parameters are available by specifying a data-dynamic="something" attribute on the endpoint element. This will expose the route parameter "something" in the .data('something') jQuery request.

The endpoint event handlers will always be passed event, req, res from the Yttrium router.

An example:

// Adding endpoints
$.route('index') // e.g. localhost/
    .append('<hello>') // localhost/hello
    .append('<param-example data-dynamic="name">'); // localhost/param-example/harry etc
    
// add handler to index route
$.route('index')
    .on('route', (e, req, res) => {
      // it's important to stop propagation of the route event
      // unless you want to trigger the parental route functions
      // which can be useful in middleware situations
      e.stopPropagation();
      return res.end('Index route!');
    });
 
// add handler to hello route
$.route('hello')
    .on('route', (e, req, res) => {
      e.stopPropagation();
      // example of sending HTML template
      $('body').html('<h1>This example uses HTML... hello world!</h1>');
      return res.end($('html').html());
    });
 
// this route responds to /param-example/:name
$.route('param-example')
    .on('route', (e, req, res) => {
      e.stopPropagation();
      const param = $.route('param-example').data('name');
      if (!param) {
        return res.end('Navigate to /param-example/anything to see dynamic routes in action')
      }
      return res.end(`I found a name! ${param}`);
    });

Query string parameters are automatically parsed and placed into an object in the data-query attribute of the route element. Accessing the query object looks like this:

$.route('query-me') // localhost/query-me
.on('route', (e, req, res) => {
  e.stopPropagation();
  const query = $.route('query-me').data('query');
  return res.end(`Get query string params: ${JSON.stringify(query)}`);
});

If you're using the Yttrium router, you can pass all requests to it like this:

const yt = require('../index');
 
const { $, server, router } = Y();
 
// ... routes ...
 
// send requests to the router
$(server).on('request', router);
 
// ... start server ...

To keep things clean, the recommended way to define routes is in a separate route file that looks like this:

module.exports = ($) => {
  $.route('index')
    .append('<hello>') // ...etc.
}

No return value is necessary because the route functions are directly manipulating the Router DOM. The routes can be loaded into the Yttrium router like this:

const yt = require('../index');
const routes = require('./routes');

const { $, server, router } = Y();

// import routes
routes($);

// ... handle requests and start server

Body Parsing

See the Yttrium body parser package for examples of parsing the request body.

Options

You can pass in an options object upon initialization:

const { $, server, router } = Y({ notFound: 'oh-noes' });

Option | Purpose -------| ------- notFound (String) | Specify the name of the 404 route (the default is not-found)

License

MIT