Pros:
Cons:
the response is a stream –> data needs to be collected in chuncks, manipulation needed to extract the JSON response.
a bit verbose
no support for promises
const https = require("https");
const url =
"[https://maps.googleapis.com/maps/api/geocode/json?address=Florence](https://maps.googleapis.com/maps/api/geocode/json?address=Florence)";
https.get(url, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
body = JSON.parse(body);
console.log(
\`City: ${body.results\[0\].formatted_address} -\`,
\`Latitude: ${body.results\[0\].geometry.location.lat} -\`,
\`Longitude: ${body.results\[0\].geometry.location.lng}\`
);
});
});
Pros:
Cons:
To install the module, run npm i request
inside your terminal.
const request = require("request");
const url =
"[https://maps.googleapis.com/maps/api/geocode/json?address=Florence](https://maps.googleapis.com/maps/api/geocode/json?address=Florence)";
request.get(url, (error, response, body) => {
let json = JSON.parse(body);
console.log(
\`City: ${json.results\[0\].formatted_address} -\`,
\`Latitude: ${json.results\[0\].geometry.location.lat} -\`,
\`Longitude: ${json.results\[0\].geometry.location.lng}\`
);
});
The first argument to request
can either be a URL string, or an object of options. Here are some of the more common options you'll encounter in your applications:
url
: The destination URL of the HTTP requestmethod
: The HTTP method to be used (GET, POST, DELETE, etc)headers
: An object of HTTP headers (key-value) to be set in the requestform
: An object containing key-value form dataconst request = require('request');
const options = {
url: '[https://www.reddit.com/r/funny.json](https://www.reddit.com/r/funny.json)',
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client' }
};
request(options, function(err, res, body) {
let json = JSON.parse(body);
console.log(json);
});
Using the options
object, this request uses the GET method to retrieve JSON data directly from Reddit, which is returned as a string in the body
field. From here, you can use JSON.parse
and use the data as a normal JavaScript object.
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing