Socket
Socket
Sign inDemoInstall

@moped/start-server

Package Overview
Dependencies
166
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @moped/start-server

This module provides an easy way to run a webpack bundle in node.js with hot reloading. It is part of the moped suite of utilities for creating composable configs for building node.js and react apps.


Version published
Weekly downloads
70
decreased by-2.78%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

start-server

This module provides an easy way to run a webpack bundle in node.js with hot reloading. It is part of the moped suite of utilities for creating composable configs for building node.js and react apps.

This module uses a custom hot reloader to do hot-reloading of individual modules if you use module.accept, and fall back to restarting the entire server if there are syntax errors or runtime errors or a module was not handled by module.accept.

Installation

yarn add --dev @moped/start-server

Usage

There are two ways to use start-server. One for HTTP servers, which renders errror pages when in the error states, and queues incoming requests during the build process. The other for any kind of node.js app.

N.B. This plugin includes the webpack.HotModuleReplacementPlugin plugin, so if you have that plugin already in your config, you must remove it.

HTTP Servers

webpack.config.js
const StartServerPlugin = require('@moped/start-server');

module.exports = {
  entry: __dirname + '/src/index.js',
  output: {
    path: __dirname + '/build',
    filename: 'server.js',
  },
  plugins: [
    new StartServerPlugin({env: {PORT: 3000}}),
  ]
};
index.js

This file just handles hot reloading and actually listening on a port. This lets webpack replace individual modules for most reloads, rather than rebooting the entire node process.

if (process.env.NODE_ENV === 'production') {
  require('./server').listen(process.env.PORT);
} else {
  const setServer = require('@moped/start-server/dev-server');
  setServer(require('./server'));

  module.hot.accept('./server', () => {
    setServer(require('./server'));
  });
}
server.js

This is your actual server. Can be an express server, or anything that exports a function that takes req and res as the two arguents.

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

module.exports = app;

Custom Node Apps

This setup automatically shuts down the node process for any compiler errors or uncaught runtime errors, restarting it on the first successful build. It will do hot module replacement wherever possible, and fall back to restarting the entire node process if hot module replacement fails.

const StartServerPlugin = require('@moped/start-server');

module.exports = {
  entry: [
    // this additional entry restarts the node process for updates
    StartServerPlugin.hotEntry,
    __dirname + '/src/index.js'
  ],
  output: {
    path: __dirname + '/build',
    filename: 'server.js',
  },
  plugins: [
    new StartServerPlugin(/* {...options} */),
  ]
};

Licence

MIT

FAQs

Last updated on 10 Jul 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc