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

servelet

v1.2.0

Published

Servelet is a simple module to serve dynamic page modules and static pages.

Downloads

6

Readme

servelet

Servelet is a simple Node.js module to serve dynamic page modules and static pages. A dynamic page module exports a function that receives a data object that is used to build the dynamic page using JavaScript template literals.

Install

npm install servelet

Example

When servelet is required, it will search all files in the 'views' and 'partials' folders for static and dynamic files. By setting the staticExt and dynamicExt options we tell the module to look for static .htm or .html, and dynamic .js files.

const servelet = require('servelet')({
  globalData: { gValue: 123 }
});

Here is an example of a dynamic .js file that uses custom data, global data, and the include method to include a file from the 'views/partials' folder. This file points out the ease of using JavaScript template literals for building dynamic content.

// index.js
// Exports a function that returns a template literal, using properties and methods of the data parameter
module.exports = (data) => `
<!DOCTYPE html>
<html lang="en">
${data.include('head')}
<body>
  <p>This is a global value: ${data.global.gValue}.</p>
  <p>These are custom values: ${(data.userName) ? `Welcome ${data.userName}` : 'Welcome Guest'}</p>
</body>
`;

Then we can serve the page in a GET request through Express, and feed in our custom values.

const app = require('express')();
app.get('/', (req, res) => {
  
  servelet.serve('index', { userName: 'Michael' }, (html) => {
    res.send(html);
  });
  
});

API

let servelet = require('servelet')(options);

Options

| Key | Description | Default | | :---: | --- | :---: | | root | The root directory if different than the current one | '' | | views | The name of the views folder | 'views' | | partials | The name of the partials folder inside the views folder | 'partials' | | staticExt | The static file extensions separated by a semi-colon | 'html' | | dynamicExt | The dynamic file extensions separated by a semi-colon | 'js' | | globalData | The data to send to all dynamic files (accessed by data.global) | '' |

Properties and Methods

.error

Get the most recent error object, or null

.ready

If the module has set up all the dynamic and static pages

.serve(page, data, callback)

Get the content of dynamic or static pages.

  • page : The page name to serve from the views folder
  • data : The object to pass into a dynamic page
  • callback : The optional callback for completion
app.get('/about', (req, res) => {
  
  const userData = getTheUserData(); // Make use of some data that alters the about page
  servelet.serve('about', { user: req.userData }, (html) => {
    res.send(html); // Send the compiled page text to the response
  });
  
});

.updateGlobalData(data)

Update the global data object that is sent to all dynamic pages.

  • data {Object} : The object to update the global data with
// app.js
const servelet = require('servelet')({
  globalData: { dynamicId: 342 }
});

servelet.updateGlobalData({ dynamicId: 738 });
servelet.serve('index', (html) => { res.send(html); });

----------

// index.js
module.exports = (data) => `
<p>The ID is: ${data.globalData.dynamicId}</p>
`;
// HTML Response: <p>The ID is: 738</p>

.on(event, callback)

Add an event listener for an event.

  • event {string} : 'error', 'warning', or 'ready'
  • callback {Function} : The callback for the event listener
servelet.on('error', myErrorCallback)
  .on('warning', (w) => { console.log('Warning: ' + w); })
  .on('ready', () => { console.log('Servelet is ready.'); });

.off(event, callback)

Remove an event listener for an event.

  • event {string} : 'error', 'warning', or 'ready'
  • callback {Function} : The callback that was used in the .on method call
servelet.off('error', myErrorCallback);

.reloadStaticPage(page, callback)

Reload a static page in the servelet cache. This is useful with a GET request to reload one or more static pages that have been altered on the server.

  • page {string|string[]} : An optional page name or array of page names
  • callback {Function(Error|null)} : The callback function for error or completion
servelet.reloadStaticPage(callback); // Reloads all static pages
servelet.reloadStaticPage('index', callback);
servelet.reloadStaticPage(['index', 'nav'], callback);

Dynamic Page Data Methods

These methods are available on the data object that is sent into the dynamic pages.

.include(page, newData)

Include a partial file.

  • page {string} : The name of the partial file to include
  • newData {Object=} : An optional object with new data for the include page
// home.js
module.exports = (data) => `
<p>Include a partial file: ${data.include('message', { name: 'Bob' })}</p>
`;

.layout(page, html)

Include a layout file for the current dynamic page.

  • page {string} : The layout name with the property name seprated by a colon
  • html {string} : The html template literal that will be injected into the layout
// index.js
// Exports a function that returns the result of the layout method call
module.exports = (data) => data.layout('layout:main', `
<p>This is the main portion for the layout file.</p>
`;

----------

// layout.js
// Uses the main property from the index.js file, and includes a partial
module.exports = (data) => `
<body>
  ${data.main}
  ${data.include('article')}
</body>
`;

License

MIT