gpt4 book ai didi

node.js - 将 bing 搜索嵌入到 Nodejs 中

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:52 25 4
gpt4 key购买 nike

我想将bing搜索嵌入到Nodejs中,我阅读了文档a link 。不过,该方法是关于php的,我找不到如何在nodejs中使用bing搜索的教程。(我是新手,我对php不熟悉。我尝试将php代码转换为nodejs,但失败了,因为两者之间有很大差异)

假设,我有这样的 bing.ejs:

<html>
<head>
<title>Bing Search Tester (Basic)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Bing Search Tester (Basic)</h1>
<form method="POST" action="/bing">
<label for="service_op">Service Operation</label><br/>
<input name="service_op" type="radio" value="Web" CHECKED /> Web
<input name="service_op" type="radio" value="Image" /> Image
<br/>
<label for="query">Query</label><br/>
<input name="query" type="text" size="60" maxlength="60" value="" /><br /><br />
<input name="bt_search" type="submit" value="Search" />
</form>
<h2>Results</h1>
{RESULTS}
</body>
</html>

如何在

中编写代码
app.post('/bing', function(req, res) {
var service_op = req.body.service_op;
var query = req.body.query;
//something to add...
});

接下来我应该写什么?或者谁能​​给我一个模板?谢谢!

最佳答案

这对我有用:

// this somewhere at the top of your code:
var acctKey = 'YourAPIKey';
var rootUri = 'https://api.datamarket.azure.com/Bing/Search';
var auth = new Buffer([ acctKey, acctKey ].join(':')).toString('base64');
var request = require('request').defaults({
headers : {
'Authorization' : 'Basic ' + auth
}
});

// here's how to perform a query:
app.post('/bing', function(req, res) {
var service_op = req.body.service_op;
var query = req.body.query;
request.get({
url : rootUri + '/' + service_op,
qs : {
$format : 'json',
Query : "'" + query + "'", // the single quotes are required!
}
}, function(err, response, body) {
if (err)
return res.send(500, err.message);
if (response.statusCode !== 200)
return res.send(500, response.body);
var results = JSON.parse(response.body);
res.send(results.d.results);
});
});

此代码使用request模块,所以先安装它:

$ npm install request

关于node.js - 将 bing 搜索嵌入到 Nodejs 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309448/

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