Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justinsisley/served-hot
Server-side hot-reloading for Express
https://github.com/justinsisley/served-hot
express hot-reloading nodejs
Last synced: about 1 month ago
JSON representation
Server-side hot-reloading for Express
- Host: GitHub
- URL: https://github.com/justinsisley/served-hot
- Owner: justinsisley
- License: mit
- Created: 2019-03-11T04:32:51.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-03T05:27:37.000Z (about 2 years ago)
- Last Synced: 2024-10-31T19:53:15.519Z (about 2 months ago)
- Topics: express, hot-reloading, nodejs
- Language: JavaScript
- Homepage:
- Size: 132 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
served hot
Server-side hot-reloading for Express
---
__served-hot__ brings the hot-reloading you know and love from the client-side to Express servers. It can greatly improve your development experience across the entire stack.
__It's not a pure drop-in solution__ (yet), and your mileage may vary depending on how you've set up your Express server. Nevertheless, this documentation will walk you through the setup and configuration it was built to function with.
Additionally, the code behind __served-hot__ is quite simple, and I encourage you to dig in and steal it as necessary in order to suit your specific needs.
---
# Table of Contents
- [Features](#features)
- [Documentation](#documentation)
- [Install](#install)
- [Configuration](#configuration)
- [Development](#development)
- [(in)Frequently Asked Questions](#faq)
- [Releases](https://github.com/justinsisley/served-hot/releases)
- [Credits](#credits)# Features
- __Vastly improves your developer experience__
- __Only watches your server-side files__ _(via Chokidar)_# Documentation
## Install
```bash
npm install served-hot
```## Configuration
__served-hot__ expects that your server-side routing, or API, is served under a root path, such as `/api`.
It also expects your server to have a root router file, which is a module that exports an Express router that all of your routes are defined on.
For example, let's say you have a server entry point at `src/index.js` that starts an Express server:
```javascript
const express = require('express');
const api = require('./api');const app = express();
// The "/api" path is defined as the root path for all server-side routing
app.use('/api', api);app.listen(8080);
```And you also have a file at `src/api.js` that exports your root router:
```javascript
const Router = require('express').Router;
const router = Router();router.get('/users', (req, res) => {
res.json(['list', 'of', 'users']);
});router.post('/hello', (req, res) => {
res.json({ data: 'world' });
});module.exports = router;
```With the above configuration, your Express server configuration is kept separate from your route definitions, which is generally considered a good practice even outside of using __served-hot__.
Using the above configuration, to enable hot-reloading for all of your Express routes, modify your `src/index.js` to use __served-hot__:
```javascript
const express = require('express');
const hot = require('served-hot');const app = express();
hot(app, {
// Tell served-hot that "/api" is your root path
rootPath: '/api',
// Tell served-hot where your root router file is
routerPath: './api',
// Tell served-hot which directory to watch for file changes
watchPath: './',
});app.listen(8080);
```Alternatively, you'll probably want to do this based on the environment, for example:
```javascript
const express = require('express');
const hot = require('served-hot');
const api = require('./api');const app = express();
const env = process.env.NODE_ENV;if (env === 'production') {
app.use('/api', api);
} else {
hot(app, {
rootPath: '/api',
routerPath: './api',
watchPath: './',
});
}app.listen(8080);
```# FAQ
Coming soon...
# Credits