2696 api browserjs fixes (#3005)
* fix: broken link (#2696). * fix: correct errors in javascript for browsers. Added token warning. (#2696) * WIP - split Javascript guide into separate pages. * WIP - Revised and split JavaScript browser * WIP fix: errors in browser and node js client library. * fix: simplify and correct JavaScript for browsers. Remove separate library entry. Assume users will prefer ESM. Simplify instructions for the browser example app. * fix: add import instruction * fix: Redo Javascript browser doc. Break down the use cases and improve examples. * fix: browser use cases for JS module distributions. * fix: browser package should be used whether bundling or not.pull/3027/head
parent
c9eed1c42e
commit
e8666ddc22
|
@ -1,180 +1,23 @@
|
|||
---
|
||||
title: JavaScript client library
|
||||
seotitle: InfluxDB JavaScript client library
|
||||
list_title: JavaScript (browser)
|
||||
title: Get started with the JavaScript client library for web browsers
|
||||
seotitle: Get started with the InfluxDB JavaScript client library for web browsers
|
||||
list_title: JavaScript for browsers
|
||||
description: >
|
||||
Use the JavaScript client library for web browsers to interact with InfluxDB.
|
||||
Use the JavaScript client library example app to interact with the InfluxDB API in web browsers.
|
||||
menu:
|
||||
influxdb_cloud:
|
||||
name: Javascript (Browser)
|
||||
parent: Client libraries
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
name: JavaScript for browsers
|
||||
identifier: client_js_browsers
|
||||
parent: Client libraries
|
||||
influxdb/cloud/tags: [client libraries, JavaScript]
|
||||
weight: 201
|
||||
aliases:
|
||||
- /influxdb/v2.0/reference/api/client-libraries/js/
|
||||
- /influxdb/cloud/reference/api/client-libraries/browserjs/
|
||||
- /influxdb/cloud/api-guide/client-libraries/browserjs/write
|
||||
- /influxdb/cloud/api-guide/client-libraries/browserjs/query
|
||||
related:
|
||||
- /influxdb/cloud/api-guide/client-libraries/nodejs/write/
|
||||
- /influxdb/cloud/api-guide/client-libraries/nodejs/query/
|
||||
---
|
||||
|
||||
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.
|
||||
|
||||
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB.
|
||||
If just getting started, see [Get started with InfluxDB](/influxdb/v2.0/get-started/).
|
||||
|
||||
## Before you begin
|
||||
|
||||
1. Install [NodeJS](https://nodejs.org/en/download/package-manager/).
|
||||
|
||||
2. Ensure that InfluxDB is running and you can connect to it.
|
||||
For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see [InfluxDB URLs](/influxdb/v2.0/reference/urls/).
|
||||
|
||||
## Easiest way to get started
|
||||
1. Clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
|
||||
2. Navigate to the `examples` directory:
|
||||
|
||||
```js
|
||||
cd examples
|
||||
```
|
||||
3. Install `yarn` or `npm` dependencies as needed:
|
||||
|
||||
```js
|
||||
yarn install
|
||||
npm install
|
||||
```
|
||||
|
||||
3. Update your `./env` and `index.html` with the name of your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and `proxy` which relies upon proxy to forward requests to the target InfluxDB.
|
||||
4. Run the following command to run the application at [http://localhost:3001/examples/index.html]()
|
||||
|
||||
```sh
|
||||
npm run browser
|
||||
```
|
||||
|
||||
## Boilerplate for the InfluxDB Javascript client library
|
||||
Use the Javascript library to write data to and query data from InfluxDB in a browser.
|
||||
|
||||
1. To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
|
||||
|
||||
```js
|
||||
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
|
||||
```
|
||||
|
||||
2. Define constants for your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and `proxy` which relies on a proxy to forward requests to the target InfluxDB instance.
|
||||
|
||||
```js
|
||||
const proxy = '/influx'
|
||||
const token = 'example-token'
|
||||
const org = 'example-org'
|
||||
const bucket = 'example-bucket'
|
||||
```
|
||||
|
||||
3. Instantiate the InfluxDB JavaScript client and pass in the `proxy` and `token` parameters.
|
||||
|
||||
```js
|
||||
const influxDB = new InfluxDB({proxy, token})
|
||||
```
|
||||
|
||||
## Write data to InfluxDB with JavaScript
|
||||
Use the Javascript library to write data to InfluxDB in a Node.js environment.
|
||||
|
||||
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`.
|
||||
2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`.
|
||||
|
||||
```js
|
||||
import {InfluxDB, Point} from '@influxdata/influxdb-client'
|
||||
|
||||
const influxDB = new InfluxDB({url, token})
|
||||
const writeApi = influxDB.getWriteApi(org, bucket)
|
||||
```
|
||||
|
||||
The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/influxdb/v2.0/reference/glossary/#point) and write it to InfluxDB using the `writePoint` method. The `tag` and `floatField` methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.
|
||||
|
||||
```js
|
||||
writeApi.useDefaultTags({location: 'browser'})
|
||||
const point1 = new Point('temperature')
|
||||
.tag('example', 'index.html')
|
||||
.floatField('value', 24)
|
||||
console.log(`${point1}`)
|
||||
|
||||
writeApi.writePoint(point1)
|
||||
writeApi.close()
|
||||
```
|
||||
|
||||
### Complete example write script
|
||||
|
||||
```js
|
||||
const influxDB = new InfluxDB({proxy, token})
|
||||
const writeApi = influxDB.getWriteApi(org, bucket)
|
||||
|
||||
// setup default tags for all writes through this API
|
||||
writeApi.useDefaultTags({location: 'browser'})
|
||||
const point1 = new Point('temperature')
|
||||
.tag('example', 'index.html')
|
||||
.floatField('value', 24)
|
||||
console.log(` ${point1}`)
|
||||
|
||||
writeApi.writePoint(point1)
|
||||
|
||||
// flush pending writes and close writeApi
|
||||
writeApi
|
||||
.close()
|
||||
.then(() => {
|
||||
console.log('WRITE FINISHED')
|
||||
})
|
||||
```
|
||||
|
||||
## Query data from InfluxDB with JavaScript
|
||||
Use the Javascript library to query data from InfluxDB.
|
||||
|
||||
1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`.
|
||||
|
||||
```js
|
||||
const queryApi = influxDB.getQueryApi(org)
|
||||
```
|
||||
|
||||
2. Create a Flux query (including your `bucket` parameter).
|
||||
|
||||
```js
|
||||
const fluxQuery =
|
||||
'from(bucket:"<my-bucket>")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r._measurement == "temperature")'
|
||||
```
|
||||
|
||||
The **query client** sends the Flux query to InfluxDB and returns line table metadata and rows.
|
||||
|
||||
3. Use the `next` method to iterate over the rows.
|
||||
|
||||
```js
|
||||
queryApi.queryRows(fluxQuery, {
|
||||
next(row: string[], tableMeta: FluxTableMetaData) {
|
||||
const o = tableMeta.toObject(row)
|
||||
// console.log(JSON.stringify(o, null, 2))
|
||||
console.log(
|
||||
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Complete example query script
|
||||
|
||||
```js
|
||||
// performs query and receive line table metadata and rows
|
||||
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
|
||||
queryApi.queryRows(fluxQuery, {
|
||||
next(row: string[], tableMeta: FluxTableMetaData) {
|
||||
const o = tableMeta.toObject(row)
|
||||
// console.log(JSON.stringify(o, null, 2))
|
||||
console.log(
|
||||
'${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
|
||||
)
|
||||
},
|
||||
error(error: Error) {
|
||||
console.error(error)
|
||||
console.log('\nFinished ERROR')
|
||||
},
|
||||
complete() {
|
||||
console.log('\nFinished SUCCESS')
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
For more information, see the [JavaScript client README on GitHub](https://github.com/influxdata/influxdb-client-js).
|
||||
{{% duplicate-oss %}}
|
||||
|
|
|
@ -6,7 +6,7 @@ menu:
|
|||
influxdb_cloud:
|
||||
name: Install
|
||||
parent: Node.js
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
influxdb/cloud/tags: [client libraries, JavaScript]
|
||||
weight: 100
|
||||
aliases:
|
||||
- /influxdb/cloud/reference/api/client-libraries/js/install
|
||||
|
|
|
@ -6,7 +6,7 @@ menu:
|
|||
influxdb_cloud:
|
||||
name: Query
|
||||
parent: Node.js
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
influxdb/cloud/tags: [client libraries, Node.js, JavaScript]
|
||||
weight: 201
|
||||
aliases:
|
||||
- /influxdb/cloud/reference/api/client-libraries/js/query
|
||||
|
|
|
@ -6,7 +6,7 @@ menu:
|
|||
influxdb_cloud:
|
||||
name: Write
|
||||
parent: Node.js
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
influxdb/cloud/tags: [client libraries, Node.js, JavaScript]
|
||||
weight: 101
|
||||
aliases:
|
||||
- /influxdb/cloud/reference/api/client-libraries/js/write
|
||||
|
|
|
@ -1,180 +1,118 @@
|
|||
---
|
||||
title: JavaScript client library
|
||||
seotitle: InfluxDB JavaScript client library
|
||||
list_title: JavaScript (browser)
|
||||
title: Get started with the JavaScript client library for web browsers
|
||||
seotitle: Get started with the InfluxDB JavaScript client library for web browsers
|
||||
list_title: JavaScript for browsers
|
||||
description: >
|
||||
Use the JavaScript client library for web browsers to interact with InfluxDB.
|
||||
Use the JavaScript client library example app to interact with the InfluxDB API in web browsers.
|
||||
menu:
|
||||
influxdb_2_0:
|
||||
name: Javascript (Browser)
|
||||
name: JavaScript for browsers
|
||||
identifier: client_js_browsers
|
||||
parent: Client libraries
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
influxdb/v2.0/tags: [client libraries, JavaScript]
|
||||
weight: 201
|
||||
aliases:
|
||||
- /influxdb/v2.0/reference/api/client-libraries/browserjs/
|
||||
- /influxdb/v2.0/api-guide/client-libraries/browserjs/write
|
||||
- /influxdb/v2.0/api-guide/client-libraries/browserjs/query
|
||||
related:
|
||||
- /influxdb/v2.0/api-guide/client-libraries/nodejs/write/
|
||||
- /influxdb/v2.0/api-guide/client-libraries/nodejs/query/
|
||||
---
|
||||
|
||||
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.
|
||||
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to interact with the InfluxDB API in browsers and front-end clients. This library supports both front-end and server-side environments and provides the following distributions:
|
||||
* ECMAScript modules (ESM) and CommonJS modules (CJS)
|
||||
* Bundled ESM
|
||||
* Bundled UMD
|
||||
|
||||
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB.
|
||||
If just getting started, see [Get started with InfluxDB](/influxdb/v2.0/get-started/).
|
||||
If you're just getting started with InfluxDB, see [Get started with InfluxDB](/{{% latest "influxdb" %}}/get-started/).
|
||||
|
||||
{{% warn %}}
|
||||
### Tokens in production applications
|
||||
{{% api/browser-token-warning %}}
|
||||
{{% /warn %}}
|
||||
|
||||
* [Before you begin](#before-you-begin)
|
||||
* [Use with module bundlers](#use-with-module-bundlers)
|
||||
* [Use bundled distributions with browsers and module loaders
|
||||
](#use-bundled-distributions-with-browsers-and-module-loaders)
|
||||
* [Get started with the example app](#get-started-with-the-example-app)
|
||||
|
||||
## Before you begin
|
||||
|
||||
1. Install [NodeJS](https://nodejs.org/en/download/package-manager/).
|
||||
1. Install [Node.js](https://nodejs.org/en/download/package-manager/) to serve your front-end app.
|
||||
|
||||
2. Ensure that InfluxDB is running and you can connect to it.
|
||||
For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see [InfluxDB URLs](/influxdb/v2.0/reference/urls/).
|
||||
For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see [InfluxDB URLs](/{{% latest "influxdb" %}}/reference/urls/).
|
||||
|
||||
## Use with module bundlers
|
||||
|
||||
If you use a module bundler like Webpack or Parcel, install `@influxdata/influxdb-client-browser`. For more information and examples, see [Node.js](/{{% latest "influxdb" %}}/api-guide/client-libraries/nodejs/).
|
||||
|
||||
## Use bundled distributions with browsers and module loaders
|
||||
|
||||
1. Configure InfluxDB properties for your script.
|
||||
|
||||
```html
|
||||
<script>
|
||||
window.INFLUX_ENV = {
|
||||
url: 'http://localhost:8086',
|
||||
token: 'YOUR_AUTH_TOKEN'
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
2. Import modules from the latest client library browser distribution.
|
||||
`@influxdata/influxdb-client-browser` exports bundled ESM and UMD syntaxes.
|
||||
|
||||
{{< code-tabs-wrapper >}}
|
||||
{{% code-tabs %}}
|
||||
[ESM](#import-esm)
|
||||
[UMD](#import-umd)
|
||||
{{% /code-tabs %}}
|
||||
{{% code-tab-content %}}
|
||||
```html
|
||||
<script type="module">
|
||||
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client-browser/dist/index.browser.mjs'
|
||||
|
||||
const influxDB = new InfluxDB({INFLUX_ENV.url, INFLUX_ENV.token})
|
||||
</script>
|
||||
```
|
||||
{{% /code-tab-content %}}
|
||||
{{% code-tab-content %}}
|
||||
```html
|
||||
<script src="https://unpkg.com/@influxdata/influxdb-client-browser"></script>
|
||||
<script>
|
||||
const Influx = window['@influxdata/influxdb-client']
|
||||
|
||||
const InfluxDB = Influx.InfluxDB
|
||||
const influxDB = new InfluxDB({INFLUX_ENV.url, INFLUX_ENV.token})
|
||||
</script>
|
||||
```
|
||||
{{% /code-tab-content %}}
|
||||
{{< /code-tabs-wrapper >}}
|
||||
|
||||
After you've imported the client library, you're ready to [write data](/{{% latest "influxdb" %}}/api-guide/client-libraries/nodejs/write/?t=nodejs) to InfluxDB.
|
||||
|
||||
## Get started with the example app
|
||||
|
||||
This library includes an example browser app that queries from and writes to your InfluxDB instance.
|
||||
|
||||
1. Clone the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
|
||||
|
||||
## Easiest way to get started
|
||||
1. Clone the [examples directory](https://github.com/influxdata/influxdb-client-js/tree/master/examples) in the [influxdb-client-js](https://github.com/influxdata/influxdb-client-js) repo.
|
||||
2. Navigate to the `examples` directory:
|
||||
|
||||
```js
|
||||
```js
|
||||
cd examples
|
||||
```
|
||||
3. Install `yarn` or `npm` dependencies as needed:
|
||||
```
|
||||
|
||||
```js
|
||||
yarn install
|
||||
npm install
|
||||
```
|
||||
3. Update `./env_browser.js` with your InfluxDB [url](/{{% latest "influxdb" %}}/reference/urls/), [bucket](/{{% latest "influxdb" %}}/organizations/buckets/), [organization](/{{% latest "influxdb" %}}/organizations/), and [token](/{{% latest "influxdb" %}}/security/tokens/)
|
||||
|
||||
3. Update your `./env` and `index.html` with the name of your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and `proxy` which relies upon proxy to forward requests to the target InfluxDB.
|
||||
4. Run the following command to run the application at [http://localhost:3001/examples/index.html]()
|
||||
4. Run the following command to start the application at [http://localhost:3001/examples/index.html]()
|
||||
|
||||
```sh
|
||||
npm run browser
|
||||
```
|
||||
|
||||
## Boilerplate for the InfluxDB Javascript client library
|
||||
Use the Javascript library to write data to and query data from InfluxDB in a browser.
|
||||
`index.html` loads the `env_browser.js` configuration, the client library ESM modules, and the application in your browser.
|
||||
|
||||
1. To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
|
||||
|
||||
```js
|
||||
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
|
||||
```
|
||||
|
||||
2. Define constants for your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and `proxy` which relies on a proxy to forward requests to the target InfluxDB instance.
|
||||
|
||||
```js
|
||||
const proxy = '/influx'
|
||||
const token = 'example-token'
|
||||
const org = 'example-org'
|
||||
const bucket = 'example-bucket'
|
||||
```
|
||||
|
||||
3. Instantiate the InfluxDB JavaScript client and pass in the `proxy` and `token` parameters.
|
||||
|
||||
```js
|
||||
const influxDB = new InfluxDB({proxy, token})
|
||||
```
|
||||
|
||||
## Write data to InfluxDB with JavaScript
|
||||
Use the Javascript library to write data to InfluxDB in a Node.js environment.
|
||||
|
||||
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`.
|
||||
2. Use the `getWriteApi` method of the instantiated InfluxDB client to create a **write client**. Provide your InfluxDB `org` and `bucket`.
|
||||
|
||||
```js
|
||||
import {InfluxDB, Point} from '@influxdata/influxdb-client'
|
||||
|
||||
const influxDB = new InfluxDB({url, token})
|
||||
const writeApi = influxDB.getWriteApi(org, bucket)
|
||||
```
|
||||
|
||||
The `useDefaultTags` method instructs the write api to use default tags when writing points. Create a [point](/influxdb/v2.0/reference/glossary/#point) and write it to InfluxDB using the `writePoint` method. The `tag` and `floatField` methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.
|
||||
|
||||
```js
|
||||
writeApi.useDefaultTags({location: 'browser'})
|
||||
const point1 = new Point('temperature')
|
||||
.tag('example', 'index.html')
|
||||
.floatField('value', 24)
|
||||
console.log(`${point1}`)
|
||||
|
||||
writeApi.writePoint(point1)
|
||||
writeApi.close()
|
||||
```
|
||||
|
||||
### Complete example write script
|
||||
|
||||
```js
|
||||
const influxDB = new InfluxDB({proxy, token})
|
||||
const writeApi = influxDB.getWriteApi(org, bucket)
|
||||
|
||||
// setup default tags for all writes through this API
|
||||
writeApi.useDefaultTags({location: 'browser'})
|
||||
const point1 = new Point('temperature')
|
||||
.tag('example', 'index.html')
|
||||
.floatField('value', 24)
|
||||
console.log(` ${point1}`)
|
||||
|
||||
writeApi.writePoint(point1)
|
||||
|
||||
// flush pending writes and close writeApi
|
||||
writeApi
|
||||
.close()
|
||||
.then(() => {
|
||||
console.log('WRITE FINISHED')
|
||||
})
|
||||
```
|
||||
|
||||
## Query data from InfluxDB with JavaScript
|
||||
Use the Javascript library to query data from InfluxDB.
|
||||
|
||||
1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`.
|
||||
|
||||
```js
|
||||
const queryApi = influxDB.getQueryApi(org)
|
||||
```
|
||||
|
||||
2. Create a Flux query (including your `bucket` parameter).
|
||||
|
||||
```js
|
||||
const fluxQuery =
|
||||
'from(bucket:"<my-bucket>")
|
||||
|> range(start: 0)
|
||||
|> filter(fn: (r) => r._measurement == "temperature")'
|
||||
```
|
||||
|
||||
The **query client** sends the Flux query to InfluxDB and returns line table metadata and rows.
|
||||
|
||||
3. Use the `next` method to iterate over the rows.
|
||||
|
||||
```js
|
||||
queryApi.queryRows(fluxQuery, {
|
||||
next(row: string[], tableMeta: FluxTableMetaData) {
|
||||
const o = tableMeta.toObject(row)
|
||||
// console.log(JSON.stringify(o, null, 2))
|
||||
console.log(
|
||||
`${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Complete example query script
|
||||
|
||||
```js
|
||||
// performs query and receive line table metadata and rows
|
||||
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
|
||||
queryApi.queryRows(fluxQuery, {
|
||||
next(row: string[], tableMeta: FluxTableMetaData) {
|
||||
const o = tableMeta.toObject(row)
|
||||
// console.log(JSON.stringify(o, null, 2))
|
||||
console.log(
|
||||
'${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
|
||||
)
|
||||
},
|
||||
error(error: Error) {
|
||||
console.error(error)
|
||||
console.log('\nFinished ERROR')
|
||||
},
|
||||
complete() {
|
||||
console.log('\nFinished SUCCESS')
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
For more information, see the [JavaScript client README on GitHub](https://github.com/influxdata/influxdb-client-js).
|
||||
|
|
|
@ -16,11 +16,6 @@ aliases:
|
|||
---
|
||||
|
||||
Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) to integrate InfluxDB into your Node.js application.
|
||||
|
||||
{{% note %}}
|
||||
{{% api/v2dot0/nodejs/install %}}
|
||||
{{% /note %}}
|
||||
|
||||
In this guide, you'll start a Node.js project from scratch and code some simple API operations.
|
||||
|
||||
{{< children >}}
|
||||
|
|
|
@ -12,6 +12,7 @@ aliases:
|
|||
- /influxdb/v2.0/reference/api/client-libraries/nodejs/install
|
||||
---
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
1. Install [Node.js](https://nodejs.org/en/download/package-manager/).
|
||||
|
@ -28,11 +29,8 @@ aliases:
|
|||
|
||||
## Install dependencies
|
||||
|
||||
{{% note %}}
|
||||
{{% api/v2dot0/nodejs/install %}}
|
||||
{{% /note %}}
|
||||
|
||||
The JavaScript client contains two packages. Add both as dependencies of your project.
|
||||
The JavaScript client contains two packages.
|
||||
Add both as dependencies of your project.
|
||||
|
||||
1. Change to your project directory:
|
||||
|
||||
|
@ -52,13 +50,23 @@ The JavaScript client contains two packages. Add both as dependencies of your pr
|
|||
npm install --save @influxdata/influxdb-client-apis
|
||||
```
|
||||
|
||||
## Configure your environment
|
||||
Set environment variables for [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and [url](/influxdb/v2.0/urls). Your application will use these to interact with the InfluxDB API.
|
||||
|
||||
```sh
|
||||
{{< api/v2dot0/env >}}
|
||||
```
|
||||
## Get started with examples
|
||||
|
||||
{{% note %}}
|
||||
The client examples include an [`env`](https://github.com/influxdata/influxdb-client-js/blob/master/examples/env.js) module for conveniently accessing environment variables.
|
||||
The client examples include an [`env`](https://github.com/influxdata/influxdb-client-js/blob/master/examples/env.js) module for accessing your InfluxDB properties from environment variables or from `env.js`.
|
||||
The examples will use these to interact with the InfluxDB API.
|
||||
{{% /note %}}
|
||||
|
||||
1. Set environment variables or update `env.js` with your InfluxDB [bucket](/influxdb/v2.0/organizations/buckets/), [organization](/influxdb/v2.0/organizations/), [token](/influxdb/v2.0/security/tokens/), and [url](/influxdb/v2.0/urls).
|
||||
|
||||
```sh
|
||||
# Environment variables
|
||||
{{< api/v2dot0/env >}}
|
||||
```
|
||||
|
||||
2. Run an example script.
|
||||
|
||||
```sh
|
||||
query.ts
|
||||
```
|
||||
{{% api/v2dot0/nodejs/learn-more %}}
|
||||
|
|
|
@ -14,13 +14,17 @@ aliases:
|
|||
|
||||
Use the Javascript library to query data from InfluxDB.
|
||||
|
||||
1. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**. Provide your InfluxDB `org`.
|
||||
|
||||
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`.
|
||||
|
||||
2. Use the `getQueryApi` method of the `InfluxDB` client to create a new **query client**.
|
||||
Provide your InfluxDB `org`.
|
||||
|
||||
```js
|
||||
const queryApi = influxDB.getQueryApi(org)
|
||||
```
|
||||
|
||||
2. Create a Flux query (including your `bucket` parameter).
|
||||
3. Create a Flux query (including your `bucket` parameter).
|
||||
|
||||
```js
|
||||
const fluxQuery =
|
||||
|
@ -31,7 +35,7 @@ Use the Javascript library to query data from InfluxDB.
|
|||
|
||||
The **query client** sends the Flux query to InfluxDB and returns line table metadata and rows.
|
||||
|
||||
3. Use the `next` method to iterate over the rows.
|
||||
4. Use the `next` method to iterate over the rows.
|
||||
|
||||
```js
|
||||
queryApi.queryRows(fluxQuery, {
|
||||
|
|
|
@ -12,7 +12,6 @@ aliases:
|
|||
- /influxdb/v2.0/reference/api/client-libraries/nodejs/write
|
||||
---
|
||||
|
||||
## Write data to InfluxDB with JavaScript
|
||||
Use the Javascript library to write data to InfluxDB in a Node.js environment.
|
||||
|
||||
1. Instantiate an `InfluxDB` client. Provide your InfluxDB `url` and `token`.
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
The examples below configure the authentication token in source code for demonstration purposes only.
|
||||
To protect your data, take the following steps:
|
||||
1. Avoid sending tokens to public clients such as web browsers and mobile apps. Regard any application secret sent to client devices as public and not confidential.
|
||||
|
||||
2. Use short-lived, [**read-only tokens**](/influxdb/v2.0/reference/cli/influx/auth/create/#create-a-read-only-authentication-token) whenever possible to prevent unauthorized writes and deletes.
|
||||
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
This library supports browser and server-side (Node.js) Javascript environments. Use the @influxdata/influxdb-client module when targeting Node.js. The module supports both CommonJS and ES Module syntax.
|
||||
|
||||
To use InfluxDB management APIs in your project, also add @influxdata/influxdb-client-apis as a dependency to your project.
|
||||
|
||||
If you target the browser or deno, use @influxdata/influxdb-client-browser. It is UMD-compatible for use with module loaders. See [Javascript (browser)](/influxdb/content/v2.0/api-guide/client-libraries/js-browser) for more information.
|
Loading…
Reference in New Issue