
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
express-reloadable
Advanced tools
Make your express application reloadable without server restart.
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.
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.
npm install express-reloadable chokidar
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
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. |
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")
})
}
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.
Express-reloadable is released under the MIT license.
FAQs
Make your express application reloadable without server restart.
The npm package express-reloadable receives a total of 89 weekly downloads. As such, express-reloadable popularity was classified as not popular.
We found that express-reloadable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.