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

server-components-express

v0.1.0

Published

Express integration plugin for server components

Downloads

11

Readme

Server Components Express Travis Build Status

Express integration plugin for Server Components

What is this?

This is a helper for using Server Components with Express. It automatically handles serving static content for you, so you can just start installing and using server components and have all the content Just Work, without having to think about how it gets delivered. It provides some helper rendering methods to make that even easier too.

Getting started

A minimal setup:

var components = require("server-components");

var express = require("express");
var app = express();

// Enable this plugin
app.use(require("server-components-express"));

// Define some components
var DemoComponent = components.newElement();
DemoComponent.createdCallback = function (document) {
    this.innerHTML = "Hello world";
};
components.registerElement("demo-component", { prototype: DemoComponent });

// Use response.renderPage(html) or response.renderFragment(html)
app.get('/', (req, res) => res.renderPage("<demo-component></demo-component>"));

app.listen(8080, () => console.log("Server ready at http://localhost:8080"));

This shows the basic setup, and requests to http://localhost:8080 subsequently will receive the rendered result:

<html>
<head></head>
<body>
    <demo-component>Hello world!</demo-component>
</body>
</html>

Serving static content

The key hidden feature server-components-express adds is effortless static content serving. Any components that internally use server-components-static to include static content will return URLs (by default) of the form /components/my-component/images/cat-picture.jpg. This plugin ensures those will automatically resolve and serve the correct files from within the corresponding component, without you having to copy those static files into your core codebase.

For example, if you install a component from NPM that includes cat-picture.jpg and an index.js like:

var components = require("server-components");

var content = require("server-components-static").forComponent("cat-component");
content.setPath(__dirname);

var CatComponent = components.newElement();
CatComponent.createdCallback = function (document) {
    var img = document.createElement("img");
    img.src = content.getUrl("cat-picture.jpg");
    this.appendChild(img);
};

components.registerElement("cat-component", { prototype: CatComponent });

You can install this from NPM, require it, use it in your HTML with <cat-component></cat-component>, and it will immediately successfully render an image tag pointing to 'cat-picture.jpg', and resolve the URL for that back to the correct file.

Enabling server-components-express lets you ignore how serving of static content like CSS and images works for any components you use. As long as they use server-components-static, everything will Just Work without any interaction.

API

expressApp.use(require("server-components-express"));

Run this to add the static files handler and response helper methods to the given express application.

response.renderPage(htmlString)

This will render the given HTML, identically to components.renderPage(html), and serve the results back in the response.

If any errors occur, the errors will be passed to your configured error handler. By default, that means in development you'll get a stack trace in your console and in the body of a 500 response, and in production you'll get a 500 response with 'Service Unavailable' and no further details.

This method returns a promise. If you'd like to do something after the response has been sent, you can chain further actions. Note that errors in rendering are already caught and handed off to the standard express error handling functionality, so catch() clauses won't typically trigger.

response.renderFragment(htmlString)

This will render the given HTML identically to [components.renderFragment(html)], and serve the results back in the response. It behaves the same as response.renderPage(html) above.