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-reloadable

v2.0.2

Published

Make your express application reloadable without server restart.

Downloads

602

Readme

Why express-reloadable

Express is a well known web application framework, but it lacks a standard way to hot reload your application code without a server restart. This hurts even more if you are used to develop with a HMR alike development workflow in your react/angular layer through webpack.

Express-reloadable is a way to get HMR like code reload in your express applications.

How does it work?

We leverage the existing router feature of express and wire it together with the fantastic file-watch library chokidar. The express router will be reloaded if we detect any file change.

Installation

npm install express-reloadable chokidar

Getting started

You will have at least two files.

./router.js

export default (app) => {
  app.get("/hello", (req, res) => {
	res.send("world")
  })
}

The router file will be reloaded whenever itself or any imported/required resources will change.

./server.js

import express from "express"
import path from "path"
import reloadable from "express-reloadable"

const app = express()

reloadable(app, {
  requireFile: path.resolve(__dirname, "./router"),
  watch: path.resolve(__dirname),
})

app.listen(9090)

Nothing really new in the server file. You bootstrap your express application as before. But you pass your application object the "reloadable()" function right before you start listening for any requests.

See the example folder for a fully working demo application

Reloadable configuration options:

|Name|Default|Description| |----|-------|-----------| |requireFile|n/a|The file to (re-)require on reload| |watch|n/a|Paths to files, dirs to be watched recursively, or glob patterns. See https://github.com/paulmillr/chokidar#api for details.| |clearIf|(file) => file.indexOf("node_modules") === -1|A reload is only triggered if this functions returns a truthy value.|

How to release allocated resources on reload?

Express-reloadable supports the notion of a tearDown function. The tearDown function is called before the reload happend and gives you a chance to release any allocated resources.

// this will be executed on every reload
let databaseConnection = initDb()

export function tearDown(){
  // release the databaseConnection on reload
  databaseConnection.release()
}

export default (app) => {
  app.get("/hello", (req, res) => {
	res.send("world")
  })
}

Integration with webpack-dev-server

Just use the existing setup hook of devServer in your webpack.config.js

import path from "path"

module.exports = {
  // ... 
  // add your existing webpack config here
  // ...
  devServer: {
    setup(app){
      let srcDir = path.resolve(__dirname, "src")
      reloadable(app, {
        requireFile: path.join(srcDir, "./router"),
        watch: srcDir,
      })
    }  
  }
}

This will add your reloadable application routes from ./router available right on the webpack-dev-server instance. Same start script, same process, same logfile ... can't be any easier.

License

Express-reloadable is released under the MIT license.