如何从本地网络中的设备访问 webpack-dev-server? [英] How to get access to webpack-dev-server from devices in local network?

查看:41
本文介绍了如何从本地网络中的设备访问 webpack-dev-server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些 webpack 开发服务器配置(它是整个配置的一部分):

There is some webpack dev server config (it's part of the whole config):

config.devServer = {
  contentBase: './' + (options.publicFolder ? options.publicFolder : 'public'),
  stats: {
    modules: false,
    cached: false,
    colors: true,
    chunk: false
  },
  proxy: [{
    path: /^/api/(.*)/,
    target: options.proxyApiTarget,
    rewrite: rewriteUrl('/$1'),
    changeOrigin: true
  }]
};

function rewriteUrl(replacePath) {
  return function (req, opt) {  // gets called with request and proxy object
    var queryIndex = req.url.indexOf('?');
    var query = queryIndex >= 0 ? req.url.substr(queryIndex) : "";
    req.url = req.path.replace(opt.path, replacePath) + query;
    console.log("rewriting ", req.originalUrl, req.url);
  };
}

我使用以下命令执行 webpack:

I execute webpack with the following command:

node node_modules/webpack-dev-server/bin/webpack-dev-server.js --host 0.0.0.0 --history-api-fallback --debug --inline --progress --config config/webpack.app.dev.js

我可以在本地机器上使用 http://localhost:8080 访问开发服务器,但我也想从我的手机、平板电脑访问我的服务器(它们在同一个 Wi-Fi 网络).

I can get access to dev server using http://localhost:8080 on my local machine, but I also want to get access to my server from my mobile, tablet (they are in the same Wi-Fi network).

我怎样才能启用它?谢谢!

How can I enable it? Thanks!

推荐答案

(如果您使用的是 Mac 并且网络和我的一样.)

(If you're on a Mac and network like mine.)

使用 --host 0.0.0.0 运行 webpack-dev-server — 这让服务器可以侦听来自网络的请求,而不仅仅是 localhost.

Run webpack-dev-server with --host 0.0.0.0 — this lets the server listen for requests from the network, not just localhost.

在网络上查找您的计算机地址.在终端中,输入 ifconfig 并查找 en1 部分或类似 inet 192.168.1.111

Find your computer's address on the network. In terminal, type ifconfig and look for the en1 section or the one with something like inet 192.168.1.111

在同一网络的移动设备上,访问 http://192.168.1.111:8080 并享受热加载开发的乐趣.

In your mobile device on the same network, visit http://192.168.1.111:8080 and enjoy hot reloading dev bliss.

这篇关于如何从本地网络中的设备访问 webpack-dev-server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆