[英]HTTP GET Request in Node.js Express
如何從 Node.js 或 Express.js 中發出 HTTP 請求? 我需要連接到另一個服務。 我希望調用是異步的,並且回調包含遠程服務器的響應。
這是我的示例中的一些代碼片段。 它是異步的並返回一個 JSON 對象。 它可以執行任何形式的 GET 請求。
請注意,有更多最佳方法(只是一個示例)-例如,不是將您放入數組中的塊連接起來並加入等等...希望它能讓您朝着正確的方向開始:
const http = require('http');
const https = require('https');
/**
* getJSON: RESTful GET request returning JSON object(s)
* @param options: http options object
* @param callback: callback to pass the results JSON object(s) back
*/
module.exports.getJSON = (options, onResult) => {
console.log('rest::getJSON');
const port = options.port == 443 ? https : http;
let output = '';
const req = port.request(options, (res) => {
console.log(`${options.host} : ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
output += chunk;
});
res.on('end', () => {
let obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', (err) => {
// res.send('error: ' + err.message);
});
req.end();
};
它是通過創建一個選項對象來調用的,例如:
const options = {
host: 'somesite.com',
port: 443,
path: '/some/path',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
並提供回調函數。
例如,在服務中,我需要上面的 REST 模塊,然后執行以下操作:
rest.getJSON(options, (statusCode, result) => {
// I could work with the resulting HTML/JSON here. I could also just return it
console.log(`onResult: (${statusCode})\n\n${JSON.stringify(result)}`);
res.statusCode = statusCode;
res.send(result);
});
如果您正在尋找async
/ await
(線性,無回調)、promise、編譯時支持和智能感知,我們創建了一個符合該要求的輕量級 HTTP 和 REST 客戶端:
嘗試在 node.js 中使用簡單的http.get(options, callback)
函數:
var http = require('http');
var options = {
host: 'www.google.com',
path: '/index.html'
};
var req = http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
console.log('BODY: ' + body);
// ...and/or process the entire body here.
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
還有一個通用的http.request(options, callback)
函數,它允許您指定請求方法和其他請求詳細信息。
Request和Superagent是很好用的庫。
注意:請求已棄用,使用風險自負!
使用request
:
var request=require('request');
request.get('https://someplace',options,function(err,res,body){
if(err) //TODO: handle err
if(res.statusCode === 200 ) //etc
//TODO Do something with response
});
您還可以使用Requestify ,這是我為 nodeJS 編寫的一個非常酷且非常簡單的 HTTP 客戶端,它支持緩存。
只需對 GET 方法請求執行以下操作:
var requestify = require('requestify');
requestify.get('http://example.com/api/resource')
.then(function(response) {
// Get the response body (JSON parsed or jQuery object for XMLs)
response.getBody();
}
);
此版本基於bryanmac最初提出的函數,它使用了 promise,更好的錯誤處理,並在 ES6 中進行了重寫。
let http = require("http"),
https = require("https");
/**
* getJSON: REST get request returning JSON object(s)
* @param options: http options object
*/
exports.getJSON = function (options) {
console.log('rest::getJSON');
let reqHandler = +options.port === 443 ? https : http;
return new Promise((resolve, reject) => {
let req = reqHandler.request(options, (res) => {
let output = '';
console.log('rest::', options.host + ':' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', () => {
try {
let obj = JSON.parse(output);
// console.log('rest::', obj);
resolve({
statusCode: res.statusCode,
data: obj
});
}
catch (err) {
console.error('rest::end', err);
reject(err);
}
});
});
req.on('error', (err) => {
console.error('rest::request', err);
reject(err);
});
req.end();
});
};
因此,您不必傳入回調函數,而是 getJSON() 返回一個承諾。 在以下示例中,該函數在 ExpressJS 路由處理程序中使用
router.get('/:id', (req, res, next) => {
rest.getJSON({
host: host,
path: `/posts/${req.params.id}`,
method: 'GET'
}).then(({ statusCode, data }) => {
res.json(data);
}, (error) => {
next(error);
});
});
出錯時,它將錯誤委托給服務器錯誤處理中間件。
Unirest是我遇到的最好的庫,用於從Node.js發出 HTTP 請求。 它旨在成為一個多平台框架,因此,如果您還需要在 Ruby、PHP、Java、Python、Objective C、.Net 或 Windows 8 上使用 HTTP 客戶端,那么了解它在 Node 上的工作原理將非常有用。 據我所知,unirest 庫主要由現有的 HTTP 客戶端支持(例如,在 Java、Apache HTTP 客戶端、Node、 Mikeal 的請求庫上) - Unirest 只是將更好的 API 放在上面。
下面是一些 Node.js 的代碼示例:
var unirest = require('unirest')
// GET a resource
unirest.get('http://httpbin.org/get')
.query({'foo': 'bar'})
.query({'stack': 'overflow'})
.end(function(res) {
if (res.error) {
console.log('GET error', res.error)
} else {
console.log('GET response', res.body)
}
})
// POST a form with an attached file
unirest.post('http://httpbin.org/post')
.field('foo', 'bar')
.field('stack', 'overflow')
.attach('myfile', 'examples.js')
.end(function(res) {
if (res.error) {
console.log('POST error', res.error)
} else {
console.log('POST response', res.body)
}
})
你可以在這里直接跳轉到 Node 文檔
查看httpreq :這是我創建的一個節點庫,因為我很沮喪那里沒有簡單的 http GET 或 POST 模塊;-)
如果您只需要發出簡單的 get 請求並且不需要支持任何其他 HTTP 方法,請查看: simple-get :
var get = require('simple-get');
get('http://example.com', function (err, res) {
if (err) throw err;
console.log(res.statusCode); // 200
res.pipe(process.stdout); // `res` is a stream
});
使用reqclient :不是為腳本目的而設計的,如request
或許多其他庫。 Reqclient 允許在構造函數中指定許多有用的配置,當您需要一次又一次地重用相同的配置時:基本 URL、標頭、身份驗證選項、日志記錄選項、緩存等。還具有查詢和 URL 解析、自動查詢編碼和JSON 解析等
使用該庫的最佳方法是創建一個模塊來導出指向 API 的對象以及連接所需的必要配置:
模塊client.js
:
let RequestClient = require("reqclient").RequestClient
let client = new RequestClient({
baseUrl: "https://myapp.com/api/v1",
cache: true,
auth: {user: "admin", pass: "secret"}
})
module.exports = client
在需要使用 API 的控制器中,如下所示:
let client = require('client')
//let router = ...
router.get('/dashboard', (req, res) => {
// Simple GET with Promise handling to https://myapp.com/api/v1/reports/clients
client.get("reports/clients")
.then(response => {
console.log("Report for client", response.userId) // REST responses are parsed as JSON objects
res.render('clients/dashboard', {title: 'Customer Report', report: response})
})
.catch(err => {
console.error("Ups!", err)
res.status(400).render('error', {error: err})
})
})
router.get('/orders', (req, res, next) => {
// GET with query (https://myapp.com/api/v1/orders?state=open&limit=10)
client.get({"uri": "orders", "query": {"state": "open", "limit": 10}})
.then(orders => {
res.render('clients/orders', {title: 'Customer Orders', orders: orders})
})
.catch(err => someErrorHandler(req, res, next))
})
router.delete('/orders', (req, res, next) => {
// DELETE with params (https://myapp.com/api/v1/orders/1234/A987)
client.delete({
"uri": "orders/{client}/{id}",
"params": {"client": "A987", "id": 1234}
})
.then(resp => res.status(204))
.catch(err => someErrorHandler(req, res, next))
})
reqclient
支持許多功能,但它有一些其他庫不支持的功能: OAuth2 集成和使用cURL 語法的記錄器集成,並且始終返回本機 Promise 對象。
如果您需要向IP
和Domain
發送GET
請求(其他答案沒有提到您可以指定port
變量),您可以使用此功能:
function getCode(host, port, path, queryString) {
console.log("(" + host + ":" + port + path + ")" + "Running httpHelper.getCode()")
// Construct url and query string
const requestUrl = url.parse(url.format({
protocol: 'http',
hostname: host,
pathname: path,
port: port,
query: queryString
}));
console.log("(" + host + path + ")" + "Sending GET request")
// Send request
console.log(url.format(requestUrl))
http.get(url.format(requestUrl), (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
console.log("GET chunk: " + chunk);
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log("GET end of response: " + data);
});
}).on("error", (err) => {
console.log("GET Error: " + err);
});
}
不要錯過文件頂部的 require 模塊:
http = require("http");
url = require('url')
還要記住,您可以使用https
模塊通過安全網絡進行通信。 所以這兩行會改變:
https = require("https");
...
https.get(url.format(requestUrl), (resp) => { ......
## you can use request module and promise in express to make any request ##
const promise = require('promise');
const requestModule = require('request');
const curlRequest =(requestOption) =>{
return new Promise((resolve, reject)=> {
requestModule(requestOption, (error, response, body) => {
try {
if (error) {
throw error;
}
if (body) {
try {
body = (body) ? JSON.parse(body) : body;
resolve(body);
}catch(error){
resolve(body);
}
} else {
throw new Error('something wrong');
}
} catch (error) {
reject(error);
}
})
})
};
const option = {
url : uri,
method : "GET",
headers : {
}
};
curlRequest(option).then((data)=>{
}).catch((err)=>{
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.