├── .gitignore ├── webpack.config.js ├── middlewares └── auth.js ├── wrangler.toml ├── routers ├── auth.js └── item.js ├── controllers ├── auth.js └── item.js ├── index.js ├── package.json └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "target": "webworker", 3 | "entry": "./index.js", 4 | "mode": "production" 5 | }; -------------------------------------------------------------------------------- /middlewares/auth.js: -------------------------------------------------------------------------------- 1 | exports.authMiddleware = (req, res) => { 2 | req.isAuthenticated = true 3 | req.username = "flaviorajta" 4 | } -------------------------------------------------------------------------------- /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "worker" 2 | type = "webpack" 3 | account_id = "_your_account_id_" 4 | workers_dev = true 5 | zone_id = "_your_zone_id_" 6 | webpack_config = "webpack.config.js" -------------------------------------------------------------------------------- /routers/auth.js: -------------------------------------------------------------------------------- 1 | const restCfWorker = require('cloudflare-worker-rest-api') 2 | const { login } = require('../controllers/auth') 3 | 4 | const router = new restCfWorker() 5 | 6 | router.post('/login', login) 7 | 8 | module.exports = router -------------------------------------------------------------------------------- /routers/item.js: -------------------------------------------------------------------------------- 1 | const restCfWorker = require('cloudflare-worker-rest-api') 2 | const { getItems, deleteItem } = require('../controllers/item') 3 | 4 | const router = new restCfWorker() 5 | 6 | router.delete('/:itemId', deleteItem) 7 | router.get('/', getItems) 8 | 9 | module.exports = router -------------------------------------------------------------------------------- /controllers/auth.js: -------------------------------------------------------------------------------- 1 | var jwt = require('jsonwebtoken') 2 | 3 | exports.login = async (req, res) => { 4 | const { username } = await req.body() 5 | 6 | if (!username) { 7 | return res.send({ status: 0, message: "Please send a username!" }, 400) 8 | } 9 | var token = jwt.sign({ username }, "helloworld") 10 | return res.send({ status: 1, token, message: `Hello ${username}` }) 11 | } -------------------------------------------------------------------------------- /controllers/item.js: -------------------------------------------------------------------------------- 1 | exports.getItems = (req, res) => { 2 | return res.send({ data: ["Item1", "Item2", "Item3"] }) 3 | } 4 | 5 | exports.deleteItem = async (req, res) => { 6 | const { itemId } = req.params 7 | 8 | if (!itemId) { 9 | return res.send({ status: 0, message: "Wrong Data!" }, 400) 10 | } 11 | 12 | return res.send({ status: 1, message: `Successfully removed ${itemId}!` }) 13 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const restCfWorker = require('cloudflare-worker-rest-api') 2 | 3 | const authRouter = require('./routers/auth') 4 | const itemRouter = require('./routers/item') 5 | const { authMiddleware } = require('./middlewares/auth') 6 | 7 | const app = new restCfWorker() 8 | 9 | // middlewares 10 | app.use(authMiddleware) 11 | 12 | // routes 13 | app.use("/auth", authRouter) 14 | app.use("/item", itemRouter) 15 | 16 | app.any("/test", async (req, res) => { 17 | body = await req.body() 18 | query = req.query() 19 | return res.send({ status: 1, message: "Healthy services!", body, query }) 20 | }) 21 | 22 | addEventListener('fetch', event => { 23 | event.respondWith(app.handleRequest(event.request)) 24 | }) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cloudflare-worker-example-rest-api", 3 | "version": "1.0.0", 4 | "description": "Cloudflare worker example app, using the cloudflare-worker-rest-api package", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "publish": "wrangler publish" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/rajtatata/cloudflare-worker-example-rest-api.git" 13 | }, 14 | "author": "Flavio Rajta", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/rajtatata/cloudflare-worker-example-rest-api/issues" 18 | }, 19 | "homepage": "https://github.com/rajtatata/cloudflare-worker-example-rest-api#readme", 20 | "keywords": [ 21 | "express", 22 | "expressjs", 23 | "cloudflare", 24 | "worker", 25 | "rest", 26 | "api" 27 | ], 28 | "dependencies": { 29 | "cloudflare-worker-rest-api": "^1.0.0", 30 | "jsonwebtoken": "^8.5.1" 31 | }, 32 | "devDependencies": {} 33 | } 34 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Cloudflare Worker Rest Api Example 2 | 3 | This project is an example of a REST api with Cloudflare Workers using [cloudflare-worker-rest-api](https://github.com/rajtatata/cloudflare-worker-rest-api) package 4 | 5 | ## Description 6 | 7 | The cloudflare-worker-rest-api package makes it very easy to build REST apis since it is very similar to express framework. 8 | 9 | ### Project Structure 10 | 11 | I have divided the project into controllers, middlewares and routers in order for it to look like something closer to real world applications.
12 | These are then imported on the `index.js` file and connected with our app. 13 | 14 | ### Installing and Deploying 15 | 16 | 1. The first thing you should do is go and create a Cloudflare Account if you have not done so 17 | 2. Next up we need to setup our machine in order to deploy our build to cloudflare 18 | - [Click Here](https://developers.cloudflare.com/workers/quickstart/) if you want to follow official setup docs 19 | 3. Start by installing wrangler globaly on your system 20 | 21 | ``` 22 | npm i @cloudflare/wrangler -g 23 | ``` 24 | 4. After that, make sure to configure the global user for wrangler 25 | - Follow on-screen instructions to get your api key 26 | ``` 27 | wrangler config 28 | Enter API token: 29 | superlongapitoken 30 | ``` 31 | 5. Now you are ready to clone this project on our machine 32 | 6. After cloning the project, make sure to edit `wrangler.toml` file 33 | - fill `account_id` you your account id 34 | - fill `zone_id` you your zone id 35 | 7. Don't forget to install necessary project packages 36 | ``` 37 | npm install 38 | ``` 39 | 8. Publish the project with wrangler 40 | - it will install packages, build, and the deploy to cloudflare 41 | ``` 42 | npm run publish 43 | ``` --------------------------------------------------------------------------------