A simple "hello world" edge worker service built with Restt and Restt-CLI.
Once you've cloned or downloaded this repository you'll want to open terminal in the project root and install the packages:
$ npm install
You should successfully have installed the required dependencies now.
Open up the src
directory and you should find a file named src/helloworld-service.js
which looks like this:
// Import the required classes from Restt
import { Restt, Service, Resource, Response } from 'restt';
// Create a hello world service
const helloworld = new Service({
// Define the origin of the service (change 'helloworld.restt.io' to your domain)
origin: 'https://helloworld.restt.io',
// Define the list of resources
resources: [
// Bind a resource to the service
new Resource({
// Define the endpoint (https://helloworld.restt.io/helloworld)
endpoint: '/helloworld',
// Define the resource protocol
method: 'GET',
// Define the response of the resource
response() {
// Create a response to send to the client
return new Response({
// Define the body of the response
body: {
message: 'hello world!'
}
});
}
})
]
});
// Create the Restt application
const app = new Restt();
// Bind the service to the application
app.use(helloworld);
Open this file in VSCode or your code editor of choice.
You'll see the origin
is being set to https://helloworld.restt.io
.
Replace helloworld.restt.io
with your domain name (e.g. avocado.unicorn.ai
) you'll be deploying to production with.
Now that you've finished setting up your project you can proceed to serving your edge worker services locally with Restt-CLI:
$ restt serve src/helloworld-service.js
Once served you can test your edge worker services are working correctly - let's connect to our resource using curl (you'll need to replace for your domain again):
$ curl http://localhost:3000/helloworld.restt.io/helloworld
If everything is working correctly then curl should output the following:
{
"message": "helloworld!"
}
Please ensure that configured cloudflare in your restt.config.json you have also registered for Cloudflare Workers on your Cloudflare account.
You're now ready to deploy your edge worker services in production with Cloudflare Workers using Restt-CLI:
$ restt deploy src/helloworld-service.js
Congratulations - you've succesfully built and shipped your services to the edge using Cloudflare Workers and Restt!
Check out the store example repository which also includes a Provider using Stripe and WorkersKV.
You can also check out the full Restt Documentation and Restt-CLI Documentation.