gpt4 book ai didi

javascript - V8 javascript 引擎中的 http 函数

转载 作者:搜寻专家 更新时间:2023-11-01 04:58:22 24 4
gpt4 key购买 nike

我想独立使用 V8 javascript 引擎,例如我将按照说明在命令行中运行它 here :

$> ./v8-shell -e 'print("10*10 = " + 10*10)'

我希望 javascript 执行一些 http 请求,最好使用 jQuery API,但 XMLHttpRequest 也可以。

V8 中是否有任何内置方法可以执行此操作?如果没有,有什么方法可以在不实现访问器/cpp 扩展的情况下实现它?

最佳答案

Is there any built in method in V8 to do this?

不直接在 V8 中,但是有 NodeJS添加网络和文件系统功能,among other features .

从文档中窃取示例:

var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};

var req = http.request(options, function(res) {

// callback invoked when response is received
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');

res.on('data', function (chunk) {

// 'data' event is fired whenever a chunk of the response arrives
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

关于javascript - V8 javascript 引擎中的 http 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com