This is a template to help you get started using the Macrometa NodeJS SDK with Cloudflare Workers. The configuration within this template relies on a custom build using webpack and an agent
override when initializing the jsC8 client.
-
A Macrometa API Key. If you already have a Macrometa account, you can generate an API key by visiting your dashboard and navigating to
Account
→API Keys
→New API Key
. If you don't have a Macrometa account, you can create a free developer account here. -
Node.js
v14.x.x
or above. We recommend installing Node with a version manager like nvm or Volta. -
This template is dependent on the Cloudflare Wrangler CLI. You can install the Wrangler CLI with
npm i @cloudflare/wrangler -g
. For more information on installing Wrangler, please visit the official docs.
To generate a local copy of this template using Wrangler:
wrangler generate my-node-cloudflare-worker-template https://github.com/macrometacorp/node-cloudflare-worker-template
💡 Further documentation for the Wrangler CLI can be found here.
The example code in src/index.js
shows how to fetch documents from a collection named locations
. To get up and running quickly, you can run npm run load-mock-data
to create a new locations
collection and populate it with mock data.
To run your Cloudflare Worker locally, make sure you've set your Macrometa API Key and collection name environment variables in the .env
file located at the root of your project.
💡 If your Worker requires additional environment variables, you can add them to the
plugins
section ofwebpack.config.js
.
You can use the wrangler dev
command to run your Cloudflare Worker locally. The command will run the build command and start on port 8787
.
Once up and running, you can visit http://localhost:8787
in your browser, and the Cloudflare Worker will return a JSON response with documents fetched from the locations
collection.
💡 Further documentation for testing your Cloudflare Worker in a local environment can be found here.
You can build and publish your Cloudflare Worker using this command:
MACROMETA_API_KEY="<YOUR API KEY HERE>" MACROMETA_COLLECTION="<YOUR COLLECTION NAME HERE>" wrangler publish
This will build your application using Webpack and publish it. If this is your first time publishing, it will create a new worker named node-cloudflare-worker-template
under your account. If your worker is successfully published, the command will output a URL that looks like https://node-cloudflare-worker-template.<your user name>.workers.dev
where your worker is available.
Alternatively, you can specify MACROMETA_API_KEY
and MACROMETA_COLLECTION
as an environment variable in the Cloudflare Dashboard and run:
wrangler publish
To learn more about what you can build on the Macrometa platform, visit our docs.