You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
listening as both https and http server simultaneously #126
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)...
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);
});
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.
Reacted by Drew Wells, Robin Willis, Tsotne Nazarashvili, Wade Peterson, Dougal Graham, Teo, Chang Wang, Colby, Stefan Knoch, Casey Cesari and 40 more
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(...).
This is actually not so rare situation 👍 I need also both http and https
Reacted by Erik Hausen, Jess, iyerusad, Dobes Vandermeer, Davit Barbakadze, Esa-Matti Suuronen, Anand Rockzz, Philipp Bauer, Paul Grime, Ante Tomić and 15 more
Reacted by Tim Knipe, Dan Burzo, Dougal Graham, Andrei Ivanovici, Chang Wang, Sean Poulter, Tatsujinichi, Bert Verhelst, Simon Siefke, Mateusz Turcza and 54 more
Reacted by Jess, Justus-Jonas Erker, Foucauld Degeorges, Roman Scher, Dragos Bostan and Nicola Pedretti
Reacted by Michael Hartmayer, Jake Lauer, Håken Lid, WBDiamond, Davit Barbakadze, Anand Rockzz, Philipp Bauer, Daniel Han, Aleksei Grushikhin, RM-Eric and 1 more
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.
Configure both HTTPS and HTTP servers explicitly.
Use the same app instance from WebpackDevServer to serve both protocols
Here's the edited code for gulp task
var webpack = require("webpack");
var WebpackDevServer = require("webpack-dev-server");
var webpackConfig = require("./webpack.config.js");
var http = require('http');
var https = require('https');
var fs = require('fs');
var gulp = require('gulp');
// Certificate files for HTTPS (use your own certificates)
var httpsOptions = {
key: fs.readFileSync('path/to/your/server.key'), // Replace with your key file
cert: fs.readFileSync('path/to/your/server.crt') // Replace with your certificate file
};
gulp.task("webpack-dev-server", function (callback) {
// Modify some Webpack config options
var myConfig = Object.create(webpackConfig);
myConfig.devtool = "#source-map";
// Start the WebpackDevServer for HTTPS
var wds = new WebpackDevServer(webpack(myConfig), {
https: httpsOptions,
publicPath: "/web", // Modify based on your setup
stats: { colors: true },
hot: true, // Enable Hot Module Replacement (optional)
historyApiFallback: true // Enable single-page app fallback (optional)
});
// Listen on HTTPS
wds.listen(8004, "localhost", function (err) {
if (err) throw err;
console.log("WebpackDevServer (HTTPS) is running on https://localhost:8004");
});
// Create an HTTP server using the same WebpackDevServer app
http.createServer(wds.app).listen(8005, "localhost", function (err) {
if (err) throw err;
console.log("HTTP server is running on http://localhost:8005");
});
callback();
Set your dev server to https and configure a cert/key, also set the port to 3001. You can use mkCert to set these up.
Then set up the additional proxy:
import*asnetfrom"net";import*astlsfrom"tls";functiontcpConnection(conn: net.Socket){conn.on('error',(err)=>{console.error('Connection error:',err);conn.destroy();});conn.once('data',function(buf){constisSSL=(buf[0]===22);letproxy: net.Socket;constaddress={host: '127.0.0.1',port: 3001}if(isSSL){proxy=net.connect(address,()=>{proxy.write(buf);conn.pipe(proxy).pipe(conn);});}else{proxy=tls.connect({
...address,// configure your CA cert that matches the cert/key from your dev server.//ca: fs.readFileSync("your-rootCA.pem")},()=>{proxy.write(buf);conn.pipe(proxy).pipe(conn);});}// Handle proxy errorsproxy.on('error',(err)=>{console.error('TLS proxy error:',err);proxy.destroy();conn.destroy();});});}net.createServer(tcpConnection).listen(3000);
The proxy will proxy un-encrypted traffic to the https dev server, and it will simply forward ssl traffic without checking SSL traffic.
@MerzDaniel Tehnically it is possible to wrap dev-server into plugin and do it, we have public API to start dev server, so you can have multiple instance of this plugin with different configuration, anyway it is on our roadmap
Activity
sokra commentedon Mar 25, 2015
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.
tommedema commentedon Apr 14, 2016
I would like to see this actually. For the command line especially.
mzedeler commentedon May 18, 2016
Does this also work with websockets?
I'm constantly getting http 404 for
http://localhost:3001/sockjs-node/info?t=<...>when i usehttp.createServer(wds.app).listen(...).Jokero commentedon Aug 16, 2017
This is actually not so rare situation 👍 I need also both http and https
Jokero commentedon Aug 19, 2017
Redirect from http to https also would be ok
kitsunekyo commentedon Mar 15, 2018
bump. i always need both environments.
is this possible yet?
Jokero commentedon Jul 31, 2018
@sokra Looks like this is not rare situation 😃
jasonjiflyer commentedon Aug 23, 2018
This is really not rare situation
ehausen commentedon Aug 30, 2018
I would also need both!
michaelhartmayer commentedon Jan 3, 2019
Yes please.
jayarjo commentedon Aug 24, 2019
We need this!
everscending commentedon Sep 4, 2019
I need this as well. Bump!
anandchakru commentedon Nov 20, 2019
another +1 for this..
12 remaining items
sandorvasas commentedon Feb 5, 2022
still need it
ruojianll commentedon Apr 18, 2022
I need it.
alexander-akait commentedon Apr 18, 2022
PR welcome, we can start 2 servers
flavianh commentedon Jun 2, 2022
Need this too
pedrettin commentedon Jun 15, 2022
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
// webpack.config.js
alexander-akait commentedon Jun 15, 2022
Why you need HTTPS on dev env? Due http2/http3 support?
pedrettin commentedon Jun 15, 2022
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
httpsservices.alexander-akait commentedon Jun 15, 2022
Why not use HTTPs and self signed certificate?
pedrettin commentedon Jun 15, 2022
that's what we do
hieuzeta commentedon Sep 19, 2023
I also need to serve both http and https, any progress on this thread ?
SachinKharote commentedon Dec 6, 2024
The key changes:
Configure both HTTPS and HTTP servers explicitly.
Use the same app instance from WebpackDevServer to serve both protocols
Here's the edited code for gulp task
var webpack = require("webpack");
var WebpackDevServer = require("webpack-dev-server");
var webpackConfig = require("./webpack.config.js");
var http = require('http');
var https = require('https');
var fs = require('fs');
var gulp = require('gulp');
// Certificate files for HTTPS (use your own certificates)
var httpsOptions = {
key: fs.readFileSync('path/to/your/server.key'), // Replace with your key file
cert: fs.readFileSync('path/to/your/server.crt') // Replace with your certificate file
};
gulp.task("webpack-dev-server", function (callback) {
// Modify some Webpack config options
var myConfig = Object.create(webpackConfig);
myConfig.devtool = "#source-map";
});
whazor commentedon Dec 11, 2024
I found a workaround.
Set your dev server to
httpsand configure a cert/key, also set the port to3001. You can use mkCert to set these up.Then set up the additional proxy:
The proxy will proxy un-encrypted traffic to the https dev server, and it will simply forward ssl traffic without checking SSL traffic.
Anubhab2003 commentedon Jan 3, 2025
Hey I am Interested in contributing this issue, How to get started ?
MerzDaniel commentedon Oct 13, 2025
This should be clearly default behavior in case https devServer is used - which is requirement for e.g. secure sameSite cookies.
Would be good to see this realized after 10 years...
alexander-akait commentedon Oct 20, 2025
@MerzDaniel Tehnically it is possible to wrap dev-server into plugin and do it, we have public API to start dev server, so you can have multiple instance of this plugin with different configuration, anyway it is on our roadmap