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

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

问题描述

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

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).

如何启用它?谢谢!

推荐答案

(如果你像我一样在Mac和网络上。)

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

使用运行webpack-dev-server - host 0.0.0.0 - 这使服务器可以侦听来自网络的请求,而不仅仅是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天全站免登陆