【问题标题】:How to configure the preact dev server to use a different address?如何配置 preact 开发服务器使用不同的地址?
【发布时间】:2022-12-01 16:22:18
【问题描述】:

默认情况下,标准 preact 设置中的开发服务器在 http://0.0.0.0:8080 或您当前的 IPv4 地址上运行:

You can view the application in browser.

Local:            http://0.0.0.0:8080
On Your Network:  http://192.168.2.105:8080

两者都不是很有用。 0.0.0.0 无法提供任何服务(至少在 macOS 上,我在所有浏览器(Chrome、Brave、FF、Safari)中得到 ERR_EMPTY_RESPONSE,并且当前 IP 地址是动态的。此外,我想使用 https。

如何更改开发服务器提供应用程序内容的地址?

【问题讨论】:

    标签: preact


    【解决方案1】:

    解决方案不是我认为的 preact.config.js,而是preact-cli,它接受许多参数来配置网络服务器:

    $ preact watch
    
        --src              Specify source directory  (default src)
        --cwd              A directory to use instead of $PWD  (default .)
        --esm              Builds ES-2015 bundles for your code  (default false)
        --clear            Clear the console (default true)
        --sw               Generate and attach a Service Worker  (default false)
        --babelConfig      Path to custom Babel config (default .babelrc)
        --json             Generate build stats for bundle analysis
        --https            Run server with HTTPS protocol
        --key              Path to PEM key for custom SSL certificate
        --cert             Path to custom SSL certificate
        --cacert           Path to optional CA certificate override
        --prerender        Pre-render static content on first run
        --prerenderUrls    Path to pre-rendered routes config  (default prerender-urls.json)
        --template         Path to custom HTML template (default 'src/template.html')
        --refresh          Enables experimental preact-refresh functionality
        -c, --config       Path to custom CLI config  (default preact.config.js)
        -H, --host         Set server hostname  (default 0.0.0.0)
        -p, --port         Set server port  (default 8080)
        -h, --help         Displays this message
    

    这样我就可以在 package.json 中为 preact 创建正确的命令:

        "scripts": {
            "build": "preact build",
            "dev": "preact watch -H localhost -p 3000 --clear false",
        },
    
    

    【讨论】:

    • CLI 标志和 preact.config.js 都可以正常工作,FWIW。 0.0.0.0 本质上意味着“这台机器上的所有地址”,所以任何地址都应该可以正常工作。不过这很奇怪,MacOS 过去肯定支持 0.0.0.0 就好了。我们在 Windows 上显示 localhost 代替它,因为历史上它是唯一有问题的那个。可能需要将其切换为!linux
    • 使用0.0.0.0 是很不寻常的。这是我第一次看到它被使用。但是,我的主要问题是我在寻找解释preact.config.json 的文档时错过了指向preact-cli 选项的单个链接。顺便提一句。 localhost 也不能使用默认值。只有在我将它设置为 watch 命令中的主机之后,事情才终于开始工作。
    最近更新 更多