webpack-dev-server icon indicating copy to clipboard operation
webpack-dev-server copied to clipboard

listening as both https and http server simultaneously

Open kwindla opened this issue 9 years ago • 30 comments

Hi,

I'm moving a project over from browserify to webpack and hit a snag this afternoon. For arcane reasons (involving chrome and atom-shell/chromium behaviors) I need both https and http instances of local dev/testing web servers.

webpack-dev-server is awesome, but I didn't find any directly supported way to instantiate on two ports at the same time.

Here's what I ended up doing (full code context, farther down below)...

webpack_dev_server.listen(8004);
http.createServer(webpack_dev_server.app).listen (8005);

I'd like to ask whether this is a reasonable solution, or whether there's a better thing I should be doing, or alternatively whether a pull request to implement simultaneous https/http at the API level would be welcome.

Below, the full gulpfile.js chunk ...

var webpack = require ("webpack");
var WebpackDevServer = require ("webpack-dev-server");
var webpackConfig = require ("./webpack.config.js");
var http = require ('http');

gulp.task ("webpack-dev-server", function (callback) {
    // modify some webpack config options
    var myConfig = Object.create (webpackConfig);
    myConfig.devtool = "#source-map";
    myConfig.debug = true;

    // Start a webpack-dev-server
    var wds = new WebpackDevServer (webpack(myConfig), {
    https: true,
        publicPath: "/web",    // +   myConfig.output.publicPath,
        stats: {
            colors: true
        }
    });
  wds.listen(8004);
  http.createServer(wds.app).listen (8005);
});

Thank you.

kwindla avatar Mar 09 '15 01:03 kwindla

Glad you found a solution, but I think the use case is too rar to add options for this to the webpack-dev-server. Even without accessing internals you could start a proxy server.

sokra avatar Mar 25 '15 13:03 sokra

I would like to see this actually. For the command line especially.

tommedema avatar Apr 14 '16 16:04 tommedema

Does this also work with websockets? I'm constantly getting http 404 for http://localhost:3001/sockjs-node/info?t=<...> when i use http.createServer(wds.app).listen(...).

mzedeler avatar May 18 '16 14:05 mzedeler

This is actually not so rare situation :+1: I need also both http and https

Jokero avatar Aug 16 '17 11:08 Jokero

Redirect from http to https also would be ok

Jokero avatar Aug 19 '17 12:08 Jokero

bump. i always need both environments. is this possible yet?

kitsunekyo avatar Mar 15 '18 13:03 kitsunekyo

@sokra Looks like this is not rare situation :smiley:

Jokero avatar Jul 31 '18 17:07 Jokero

This is really not rare situation

jasonjiflyer avatar Aug 23 '18 05:08 jasonjiflyer

I would also need both!

ehausen avatar Aug 30 '18 11:08 ehausen

Yes please.

michaelhartmayer avatar Jan 03 '19 19:01 michaelhartmayer

We need this!

jayarjo avatar Aug 24 '19 11:08 jayarjo

I need this as well. Bump!

everscending avatar Sep 04 '19 16:09 everscending

another +1 for this..

anandchakru avatar Nov 20 '19 23:11 anandchakru

So a trick would be to configure nginx and open the port 80 to reroute to the actual ssecure locatio. Something like the following in your nginx configuration server { listen 80; return 301 https://www.example.com$request_uri; }

This works for me, but I would like to know If i can remove the dependency on nginx and only configure web pack. Thanks.

kmukthi avatar Nov 21 '19 20:11 kmukthi

For me - we need option to:

  • run both server
  • allow to redirect traffic from one to two (http -> https)

alexander-akait avatar Nov 19 '20 16:11 alexander-akait

Having the option to redirect http to https would be quite helpful. Now I have to always add the https to the beginning of my development links in the browser.

rightaway avatar Dec 22 '20 23:12 rightaway

+1. I am using this to test development of an app to communicate with people who are not technically sophisticated. Easier to get them to type "devsite.com" into address bar than "https://devsite.com."

natkuhn avatar Jan 25 '21 21:01 natkuhn

Bump priority. it was a surprise to me that such a functionality is not out-of-the-box.

extempl avatar Mar 12 '21 17:03 extempl

Now that Chrome is blocking third party cookies unless they are secure, I think this feature is more important for developer experience since it will become more common for devs to have a need for https during dev.

taylorgoolsby avatar Mar 29 '21 19:03 taylorgoolsby

Bump priority. We NEED this feature.

kamilic avatar Nov 03 '21 02:11 kamilic

still need it

sandorvasas avatar Feb 05 '22 12:02 sandorvasas

I need it.

ruojianll avatar Apr 18 '22 10:04 ruojianll

PR welcome, we can start 2 servers

alexander-akait avatar Apr 18 '22 13:04 alexander-akait

Need this too

flavianh avatar Jun 02 '22 08:06 flavianh

I consider this a hack, so i still think a proper webpack property to allow redirect is needed, but here is a solution only using webpack and without having to spin up another local server frontend by nginx.

Basic idea: spin up 2 webpack dev servers, 1 listening on http one on https. Have the one listening on http implement a proxy to redirect to https.

// package.json

"server": "webpack serve --config webpack.config.js --env https=false & webpack serve --config webpack.config.js --env https=true",

// webpack.config.js

module.exports = (env) => {
	return {
		....
		devServer: {
			https: env && env.https === "true",
			...
			port: env && env.https === "true" ? 443 : 80,
			...
			proxy: !env || env.https === "false" ? {
				"/**": {
					target: "https://localhost",
					secure: false, // allows the https server to not have a proper certificate
				}
			} : {}
		},
		...
	};

}

pedrettin avatar Jun 15 '22 19:06 pedrettin

Why you need HTTPS on dev env? Due http2/http3 support?

alexander-akait avatar Jun 15 '22 20:06 alexander-akait

I am guessing there are multiple use cases, but we need it to communicated with the auth service which does not do CORS with non https services.

pedrettin avatar Jun 15 '22 20:06 pedrettin

Why not use HTTPs and self signed certificate?

alexander-akait avatar Jun 15 '22 20:06 alexander-akait

that's what we do

pedrettin avatar Jun 15 '22 20:06 pedrettin

I also need to serve both http and https, any progress on this thread ?

hieuzeta avatar Sep 19 '23 13:09 hieuzeta