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

express-includes

v0.1.5

Published

Express middleware for easily including stylesheets and javascript

Downloads

8

Readme

express-includes

Build Status Coverage Status

Express includes is a module for easily including stylesheets and javascript into your views. The idea is setting up configurations for these three levels:

  • global: all pages
  • section: matching a substring of the url
  • page: matching a specific page url

Installation

npm install express-includes --save

Configuration

Here's a sample of how to get this working. You can define global styles like this:

var globalStyles = [
    "style.css"
];

var globalScripts = [
    "script.js"
];

Here's a sample of section definitions, which attempt to match sections of a website with the current url. For instance, /section below would match with /section, /section/page and /section/another/page; but wouldn't match with /other-section.

var sectionDefinition = [
    {
        url: "/section",
        styles: [ "section.css" ],
        scripts: [ "section.js" ]
    },
    {
        url: "/another/section",
        styles: [ "anothersection.css" ],
        scripts: [ "anothersection.js" ]
    }
];

And specific page definitions, which match exactly with the URL. You can also match with a page that has a wildcard (ex. an ID value) with a semicolon (:).

var pageDefinition = [
    {
        url: "/",
        styles: [ "index.css" ],
        scripts: [ "index.js" ]
    },
    {
        url: "/section/page",
        styles: [ "page.css" ],
        scripts: [ "page.js" ]
    },
    {
        url: "/section/page/:",
        styles: [ "param.css" ],
        scripts: [ "param.js" ]
    }
];

After everything is defined, create a new ExpressIncludes instance by requiring the library where you're initializing express:

var ExpressIncludes = require('express-includes');

var includes = new ExpressIncludes({
    globalStylesheets: globalStylesheets,
    globalScripts: globalScripts,
    sectionDefinition: sectionDefinition,
    pageDefinition: pageDefinition
});

Then use the middleware function provided in your express configuration, which will attach the scripts and the styles to the express res.locals object, giving you easy access to it in your view (or other middlewares).

app.use(includes.mwFn);

Using in your view

Example using jade.

each style in styles
    link(rel='stylesheet', href=style)
each script in scripts
    script(type='text/javascript', src=script)

License

MIT