V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
p2227
V2EX  ›  Node.js

child_process.spawn 参数传输的问题

  •  
  •   p2227 · 2017-08-25 19:00:09 +08:00 · 2293 次点击
    这是一个创建于 2406 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我本地有一个 https 服务,访问网址是 https://127.0.0.1:8080/

    然后在命令行中直接敲

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --host-rules="MAP xxx.asdf:443 127.0.0.1:8080" --ignore-certificate-errors --incognito https://xxx.asdf://xxx.asdf
    

    是会打开一个隐身窗口,并且打开 https://xxx.asdf,然后内容是我 https://127.0.0.1:8080/ 的那网站

    但是如果换成以下代码

    const child_process = require('child_process');
    
    child_process.spawn(
    	`/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome`,
    	[
    		`--host-rules="MAP xxx.asdf:443 127.0.0.1:8080"`,
    		`--ignore-certificate-errors`,
    		`--incognito`,
    		`https://xxx.asdf`
    	]
    )
    

    然后通过 node 去运行,会打开一个隐身窗口,但是会提示找不到 https://xxx.asdf 的 ip 地址,即无法访问

    初步看来是因为 spawn 运行的和直接在命令行里面敲有点不一样,大家知道怎么改 node 的代码达到跟命令行一样的效果吗?

    4 条回复    2017-08-28 10:40:09 +08:00
    CDL
        1
    CDL  
       2017-08-25 19:14:37 +08:00
    加上{shell:true}参数试试
    p2227
        2
    p2227  
    OP
       2017-08-25 21:35:34 +08:00
    @CDL 不行,直接是没有反应,浏览器没有打开命令行就完成了。google 了几篇文章都是说 window 下双引号的问题,但我的明明是 mac [捂脸]
    bertonzh
        3
    bertonzh  
       2017-08-25 23:17:12 +08:00
    `--host-rules`,
    `MAP xxx.asdf:443 127.0.0.1:8080`

    呢?
    p2227
        4
    p2227  
    OP
       2017-08-28 10:40:09 +08:00
    @bertonzh 不行,
    ```
    [ '--incognito',
    '--host-rules="MAP',
    'xxx.asdf:443',
    '127.0.0.1:8080"',
    '--ignore-certificate-errors',
    'https://xxx.asdf' ]
    ```
    这样也不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5161 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:42 · PVG 17:42 · LAX 02:42 · JFK 05:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.