question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

need your help for hot-reloading server with webpack

See original GitHub issue

Hi everyone!

After reading the article Hot reload all the things! I’ve created a simple koa-hot-reloading-server.

Unfortunately it doesn’t work while changes are recompile. original server based on express works fine express-hot-reloading-server

the main thing is in this code:

import http from 'http'
import app from './server'

const server = http.createServer(app)
let currentApp = app
server.listen(3000)

if (module.hot) {
 module.hot.accept('./server', () => {
  server.removeListener('request', currentApp)
  server.on('request', app)
  currentApp = app
 })
}

I’ve translated it into koa:

import http from 'http';
import app from './server';

const port = 3000;

const appCallback = app.callback();
const server = http.createServer(appCallback);

let currentApp = appCallback;

server.listen(port);

if (module.hot) {
    module.hot.accept('./server', () => {
        server.removeListener('request', currentApp);
        server.on('request', appCallback);
        currentApp = appCallback;
    });
}

console.log(`Server running on: http://localhost: ${port}`);

it serves requests but doesnt reload servers new code, e.g. these lines are not have an action

        server.removeListener('request', currentApp);
        server.on('request', appCallback);
        currentApp = appCallback;

Help me please

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bttmlycommented, Mar 8, 2017

You need to call app.callback() upon module.hot.accept. A difference between Koa and Express is that express “instances” are actually functions (which can be passed to http.createServer directly) with extra properties on them, whereas new Koa() returns an object, with a method (called callback) which returns a function for use with http.createServer.

This should work:

import http from 'http';
import app from './server';

const port = 3000;

let currentApp = app.callback();
const server = http.createServer(currentApp);

server.listen(port);

if (module.hot) {
    module.hot.accept('./server', () => {
        server.removeListener('request', currentApp);
        currentApp = app.callback();
        server.on('request', currentApp);
    });
}

console.log(`Server running on: http://localhost: ${port}`);
0reactions
sibeliuscommented, Nov 20, 2018

@jonathanong does node-dev works well with babel 7?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Module Replacement - webpack
Hot Module Replacement (or HMR) is one of the most useful features offered by webpack. It allows all kinds of modules to be...
Read more >
How to have hot reload when using "webpack serve" on ...
Basically, are you booting your localhost by running a script (e.g: node fileName.js ), or are you using your CLI to run a...
Read more >
How to hot-reload with Webpack? - codegodzilla.com - Medium
Prerequisites: Make sure you understand the basics of how to use TypeScript with Webpack. For that You can read my last lesson, here....
Read more >
Solution for Webpack 5 Dev Server Not Live Reloading
There is currently a bug with Webpack 5 and webpack -dev- server not working that is caused by the existence of a browserslist...
Read more >
10 - webpack dev server - serve content and hot reload it
Your browser can't play this video. Learn more. Switch camera.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found