Skip to content

Macrometacorp/node-cloudflare-worker-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Macrometa Node SDK Cloudflare Worker Template

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.

Requirements 🥁

  1. A Macrometa API Key. If you already have a Macrometa account, you can generate an API key by visiting your dashboard and navigating to AccountAPI KeysNew API Key. If you don't have a Macrometa account, you can create a free developer account here.

  2. Node.js v14.x.x or above. We recommend installing Node with a version manager like nvm or Volta.

  3. 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.

Installation ⚡

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.

Development 💾

1. Load Sample Data ⚗️

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.

2. Set Environment Variables 🌵

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 of webpack.config.js.

3. Run the Worker 🚂

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.

Macrometa with Cloudflare Worker

💡 Further documentation for testing your Cloudflare Worker in a local environment can be found here.

Publishing 🚀

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.

About

A template for using the Macrometa Node SDK in a Cloudflare Worker

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published