child_process.spawn 参数传输的问题

2017-08-25 19:00:09 +08:00
 p2227

我本地有一个 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 的代码达到跟命令行一样的效果吗?

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

呢?
p2227
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' ]
```
这样也不行

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/385820

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX