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

jhp-serve

v1.0.2

Published

JHP: Hypertext Preprocessor

Downloads

4

Readme

What is this?

JavaScript template strings work pretty elegantly for HTML rendering, but setting up and working on a JavaScript web server, while fairly simple, doesn't quite match the delightful simplicity of doing the same with PHP.

This project aims to recreate that PHP experience, in JavaScript.

Usage

Running the server

npx jhp-serve ./my-proj  # Assumes you have Node/npm installed

Project structure

Example:

my-proj/
  index.html.js
  about.html.js
  contact.html.js
// index.html.js

module.exports = (req) =>
  `<h1>Hello!</h1>
   <p>
     Thanks for visiting the page at ${req.path}
   </p>`

A jhp project is just a directory containing .html.js files and/or static files. .html.js files are analogous to .php files, in that each handles one route and (usually) renders one page. For both kinds of files, the route is the same as the subdirectory relative to the project root. You do not need to restart the server to test changes made to your .html.js files.

Each .html.js file exports a single function which returns either an HTML string, or a Promise of an HTML string. The latter is useful if, for example, you need to query a database before rendering the HTML.

Render functions are passed two optional arguments, req and res, which are the ExpressJS Request and Response objects. The response's content type has already been set to HTML, so that the common case of returning an HTML string doesn't have to mess with it. The request body is parsed as being "form-encoded", meaning it will work as expected for native HTML forms.

Your .html.js files are just regular JavaScript files, so you can import libraries or do whatever additional logic you may want to do in them.

A full example site can be found here: https://github.com/brundonsmith/jhp-example-site

Tips

Your JavaScript functions can construct their HTML strings in whatever way you choose, but for the most PHP-like experience I recommend using arrow functions with template string literals for basic cases.

When it comes to syntax highlighting of the HTML itself, your mileage may vary depending on your editor. From a quick search it appears that Atom may have this built-in. VSCode at least has some great extensions that give the template strings first-class status as HTML code including syntax highlighting, formatting, autocomplete, etc. Here are a couple of them:

If you plan on having additional server code, I recommend making your JHP site a subdirectory of your overall project, so that your server code and config files aren't served publicly on the web.