⛅ Cloudflare Workers as a Web Server (with Webpack) #serverless (lesson 2)

Joel Codes
16 Nov 202012:45

Summary

TLDRThis tutorial video guides viewers on setting up a Cloudflare worker as a web server, highlighting the possibility of following along without a Cloudflare account and the availability of free hosting on their platform. The presenter covers installing the 'wrangler' CLI, configuring Webpack, creating HTML files, setting up routing, and utilizing Cloudflare's worker router for efficient request handling. The video also includes refactoring the application for better structure and organization, with a focus on importing HTML, creating pages, and custom routing. Viewers are encouraged to subscribe for more Cloudflare worker content and to check out the provided links for full source code and articles.

Takeaways

  • 🚀 Configuring a Cloudflare worker to run as a web server.
  • 🔗 You can follow along without signing up for Cloudflare, and you can use the free plan for hosting.
  • 📹 This is the second video in a series on Cloudflare workers, with the first being a getting started video.
  • 🛠️ Install Wrangler globally as the CLI tool for building the website.
  • 🔧 Create a `webpack.config.js` file with the target set to web worker and set the entry point to the index file.
  • 📁 Move the index file into the source directory and include HTML loader in the webpack configuration.
  • 📝 Create `index.html` and `404.html` files in the HTML directory.
  • 📜 Use a router from the Cloudflare worker template to handle routing.
  • 🔄 Refactor the application by moving response code into separate files and organizing pages in a source directory.
  • 💻 The final setup includes importing the necessary responses and pages and ensuring correct routing.
  • 📢 Subscribe and click the bell icon for notifications about future videos and check the description for article links and source code.

Q & A

  • What is the main topic of the video?

    -The video is about configuring a Cloudflare Worker to run as a web server.

  • What are the two fun things mentioned about using Cloudflare Worker?

    -The two fun things mentioned are that viewers can follow along without signing up for Cloudflare, and when they decide to publish on Cloudflare, they can use the free plan for free hosting.

  • What is the prerequisite video recommended by the speaker?

    -The speaker recommends the 'Getting Started' video on Cloudflare Workers as a prerequisite.

  • What is the first step in setting up the project?

    -The first step is to install Wrangler globally, which is the CLI used to build the website.

  • What does the speaker do after installing Wrangler?

    -The speaker runs 'wrangler generate' to create a site called 'my website', then navigates into the project directory and reloads VS Code.

  • Why is Webpack configured in the project?

    -Webpack is configured to set the target to a web worker and to set the entry point to the index file, allowing for the loading of HTML files as templates.

  • What loader is installed to handle HTML files in the project?

    -The HTML loader is installed to handle HTML files in the project.

  • What is the purpose of creating an 'html' directory and what files are created there?

    -The 'html' directory is created to store HTML files. The speaker creates 'index.html' and '404.html' files there.

  • How does the speaker handle routing in the initial setup?

    -The speaker uses a simple routing mechanism by extracting the pathname from the request URL and returning the appropriate HTML file based on the pathname.

  • What changes are made to improve the routing system?

    -The speaker introduces a more sophisticated routing system by copying the 'router.js' from the Cloudflare Worker template and using it to handle routing.

  • What is the purpose of creating a 'lib' directory and what is added there?

    -The 'lib' directory is created to store utility functions and the speaker adds a 'router.js' file there for routing purposes.

  • How does the speaker refactor the application to improve its structure?

    -The speaker refactors the application by creating a 'pages' directory for page-specific functions and a 'responses' file in the 'lib' directory for response handling functions.

  • What is the final step the speaker suggests for viewers who want to deploy their project?

    -The speaker suggests checking out the 'Getting Started' video for more details on deploying the project onto the Cloudflare Workers platform.

Outlines

00:00

🛠️ Setting Up Cloudflare Worker as a Web Server

This paragraph introduces the video's objective to configure a Cloudflare worker as a web server. It highlights the ease of following along without a Cloudflare account and the availability of a free hosting plan. The video is the second in a series on Cloudflare workers, and while the first video is recommended, it's not mandatory. The speaker briefly mentions their background before moving on to the technical setup, which includes installing Wrangler, a CLI tool for building the website. The focus then shifts to configuring Webpack, setting up the project structure, and implementing routing with HTML file handling.

05:01

📚 Implementing Advanced Routing with Cloudflare Worker

The speaker discusses the process of enhancing the Cloudflare worker's routing capabilities by adopting a smarter routing approach from the Cloudflare worker template. They demonstrate how to integrate a router into the project, refactor the code to handle GET requests and unmatched routes, and ensure the router is properly utilized. The paragraph also covers the refactoring of responses into a separate file for better organization and the creation of a 'pages' directory to modularize the routing logic. The speaker ensures that the refactoring maintains the functionality of serving HTML files and custom routing.

10:03

🎉 Wrapping Up and Encouraging Further Engagement

In the concluding paragraph, the speaker expresses satisfaction with the progress made in setting up the Cloudflare worker, which now includes HTML importing, page creation, and custom routing. They provide guidance on deploying the project onto the Cloudflare workers platform and encourage viewers to watch the 'getting started' video for more details. The speaker also prompts viewers to subscribe for updates, engage with notifications, and follow along with an article and source code provided in the video description. They invite questions and suggestions for future videos in the comments or on Twitter, and thank the viewers for watching, signaling the end of the video.

Mindmap

Keywords

💡Cloudflare Worker

Cloudflare Worker is a serverless computing platform that allows developers to run JavaScript code directly at Cloudflare's edge network. In the video, the host is configuring a Cloudflare Worker to function as a web server, demonstrating how it can be used to serve web content. This is a key concept as it underpins the entire project being discussed.

💡Wrangler

Wrangler is the command-line interface (CLI) tool used to build and deploy Cloudflare Workers. The script mentions installing Wrangler globally, which is essential for setting up the project. Wrangler is used to generate a new site, manage dependencies, and preview the worker locally, making it a central tool in the development process.

💡Webpack

Webpack is a module bundler that is used in the video to compile and bundle the project's assets, such as JavaScript and HTML files. The script includes configuring Webpack to target a web worker and setting the entry point to the index file. This is crucial for defining how the application's resources are managed and served.

💡HTML Loader

HTML Loader is a Webpack loader that enables the loading of HTML files as templates. In the script, the host installs HTML Loader to include HTML files in the Webpack configuration, which is necessary for serving HTML content through the Cloudflare Worker.

💡wrangler.toml

wrangler.toml is the configuration file for a Cloudflare Worker project. The video script mentions modifying this file to change the project type from 'javascript' to 'webpack' and to specify the Webpack configuration file. This file is essential for defining project settings and build configurations.

💡Routing

Routing in the context of web development refers to how an application handles different URLs and requests. In the video, the host sets up routing to direct requests to the appropriate HTML content, such as serving 'index.html' for the root path and a '404.html' for not found errors. This is a fundamental aspect of web server functionality.

💡HTML5 Template

An HTML5 template is a starting point for creating HTML documents that adhere to the HTML5 standard. The script mentions using an HTML5 template for the 'index.html' and '404.html' files, which provides a basic structure for the web pages served by the Cloudflare Worker.

💡Content-Type

The Content-Type header in HTTP responses specifies the type of content that the server returns. In the video, the host changes the Content-Type to 'text/html' to ensure that the HTML content is served with the correct MIME type, which is crucial for browsers to correctly interpret and display the content.

💡Router.js

Router.js is a utility in the Cloudflare Workers template that provides a simple way to handle routing. The script mentions copying from Router.js to implement custom routing in the project, which helps in managing different routes and responses more efficiently.

💡Refactoring

Refactoring in software development is the process of restructuring existing code without changing its external behavior to improve its internal structure. In the video, the host refactors the code by moving routes and responses into separate files, which enhances readability and maintainability of the code.

💡Pages

In the context of web development, pages refer to individual HTML documents or components that make up a website. The script discusses creating a 'pages' directory to organize the HTML content, such as 'index.js' and '404.js', which represent different pages served by the Cloudflare Worker.

Highlights

Learn how to configure a Cloudflare Worker to run as a web server.

Follow along without having to sign up for Cloudflare initially.

Use Cloudflare's free plan to publish your web server.

Introduction to Wrangler CLI, the tool used to build the website.

Step-by-step guide on setting up a new site called 'my website' using Wrangler.

Configuring Webpack for Cloudflare Workers with a specific webpack.config.js file.

Installation and configuration of the HTML loader for Webpack.

Creating HTML files for the web server, including index.html and 404.html.

Setting up routing for the web server to serve different HTML files.

Using the Cloudflare Worker template router to handle routing efficiently.

Refactoring code by moving routes and responses into separate files.

Creating reusable response functions such as htmlResponse and notFoundResponse.

Organizing HTML pages into a 'pages' directory for better structure.

Importing and using these organized pages and response functions in the main index file.

Testing and ensuring the Cloudflare Worker is serving the correct HTML files.

Final demonstration of the working Cloudflare Worker with custom routing.

Encouragement to watch the first video in the series for additional context.

Information on deploying the Cloudflare Worker onto Cloudflare's platform.

Invitation to subscribe for more videos on Cloudflare Workers.

Links to the full source code and additional resources provided in the video description.

Transcripts

play00:00

hello friends in today's video i'll be

play00:02

configuring a cloudflare worker to run

play00:04

as a web server

play00:05

and two fun things about this is that

play00:07

you can follow along without even having

play00:09

to sign up for cloudflare and when you

play00:11

do want to publish it on their service

play00:12

you can use the free plan and who

play00:14

doesn't like free hosting as a reminder

play00:16

this is the second video in a series i'm

play00:18

doing on cloudflare workers

play00:20

if you haven't seen the first one i

play00:21

recommend checking it out it's the

play00:22

getting started video

play00:24

it's not a prerequisite and you can

play00:26

follow along without having seen that

play00:27

video first but i'd still recommend

play00:29

checking it out afterwards

play00:30

but first a little bit about myself

play00:35

let's just move on and if you skip the

play00:38

first video

play00:38

you'll need to install wrangler globally

play00:41

this is the cli we'll be using to build

play00:43

our website

play00:44

now i already have wrangler installed so

play00:46

i'm not going to run this command

play00:48

what i'm going to do is i'm going to run

play00:49

wrangler with generate and i'm going to

play00:51

tell it

play00:52

i'm going to create a site called my

play00:53

website

play00:55

so i'm going to run that now and i'm

play00:56

going to cd into

play00:59

my website and then i'm just going to

play01:01

reload vs code

play01:04

the first thing i need to do is

play01:06

configure webpack so i'm going to create

play01:08

a

play01:10

webpack.config.js file and paste this

play01:12

in now the key takeaways here are that

play01:14

the target is set to a web worker

play01:17

and i've set my entry point to my index

play01:20

file

play01:21

so what i'm going to do here is i'm

play01:23

going to create that source directory

play01:26

and move the index file into that source

play01:30

because i'm planning on loading html

play01:32

files as templates

play01:33

i'm going to have to include that in my

play01:35

webpack so i'm going to go ahead and

play01:37

install the html loader

play01:42

and i'll add the appropriate settings

play01:43

for html loader here

play01:45

and i'll be sure to include links to all

play01:47

of these files in the description below

play01:50

okay now that webpack has been

play01:52

configured i'm going to open up the

play01:53

wrangler tomml file

play01:55

and i'm going to change the type of the

play01:57

project from javascript

play01:59

to webpack and make sure that i have the

play02:02

webpack config set to

play02:05

webpack.config.js okay let's

play02:08

clean this up a bit i think i can make

play02:09

this a little bit larger for you guys

play02:11

there we go next i'm going to create

play02:13

those html files

play02:14

so i'm just going to create an html

play02:16

directory

play02:17

and the first one i'm going to create is

play02:20

called

play02:21

index.html and i'm going to use the

play02:25

html5

play02:26

template and i'm just going to put a

play02:31

hello index just so that we know that

play02:33

we're on the index page

play02:34

and the next file i like to create is

play02:36

going to be a 404.html and i'm going to

play02:40

do the same thing here

play02:42

except for this title i'm going to say

play02:45

404

play02:46

not found okay i'm going to head back to

play02:49

index so first i'm going to go ahead and

play02:52

close

play02:52

these open up index

play02:56

and import my new files at the top so

play02:58

i'm going to

play03:00

import index from

play03:04

and i'm going to go into the html

play03:06

directory and i'm going to load

play03:08

index.html and i'm going to say

play03:11

the same thing for my 404 page

play03:16

and now that these are loaded all i have

play03:18

to do is set up my routing so i'm going

play03:19

to say

play03:21

const path name equals

play03:24

new url from request.url

play03:29

and all this is doing is it's just

play03:30

pulling the path name out of the request

play03:32

url

play03:34

now i can use this to test the path name

play03:37

so if it equals forward slash which is

play03:39

my index

play03:40

route what i'm going to do is return

play03:44

my index

play03:47

so i'm going to take the index from here

play03:49

and instead of returning

play03:50

hello worker i'll just return index and

play03:54

otherwise i'm going to return my not

play03:58

found

play04:00

and i also want to change this to have a

play04:02

status

play04:03

of 404 and if this all works i should be

play04:06

able to open up my terminal

play04:09

and run wrangler preview watch

play04:15

and if everything works you should get a

play04:17

window that looks like this

play04:19

and i can see here it looks like it's

play04:20

returning my html as text

play04:23

so all i have to do to change that is

play04:26

change the content type to

play04:29

text html and i'm going to do that for

play04:32

the 404 also

play04:34

and now when i go back hello index

play04:38

let's test my not found

play04:42

404 not found and we could stop here but

play04:45

i don't like the way that we're doing

play04:46

the routing

play04:47

i think that there's some much smarter

play04:49

ways to do routing out there

play04:50

and what i'm actually going to do is

play04:52

copy from

play04:54

the cloudflare worker worker template

play04:57

router

play04:58

they have a router.js here that i'm

play05:01

going to copy from

play05:03

so i'm going to copy this and go back

play05:06

into my project

play05:08

and i'm going to create a lib directory

play05:11

where i'm going to create the

play05:12

router and i'm just going to paste

play05:16

everything into there going to import

play05:19

router at the top

play05:25

and then inside of my handle request i

play05:28

can just type in

play05:30

const router equals new router

play05:34

and we won't be using the path name

play05:35

anymore because the router is going to

play05:37

handle that

play05:38

and now all i have to do is say

play05:40

router.get because this is going to be

play05:43

a get request it's going to handle the

play05:45

slash

play05:46

and then the handler is going to return

play05:51

my response

play05:55

so i can get rid of this

play05:59

the next piece i'm going to do is say

play06:00

router.all

play06:03

which will handle any requests not

play06:05

matched by a previous router

play06:07

and that also has a handler

play06:10

which is going to return my 404

play06:14

and the last piece is we have to tell

play06:16

the router to actually route

play06:17

so i will say the response equals await

play06:22

router dot route and

play06:25

i'm going to pass the request into this

play06:27

and then i have to return

play06:29

the response now let's head back here

play06:31

just to make sure everything's still

play06:32

working so i'm going to hit refresh

play06:34

and it refreshed with index and my 404

play06:38

is working too so that's it really our

play06:40

cloudflare worker is now working and it

play06:42

serves our html files

play06:44

but i feel like this application does

play06:46

deserve a little bit of refactoring so

play06:47

that's what i'm going to do right now

play06:49

and i'm going to start by these

play06:50

responses i don't like having all of the

play06:52

code in this page here

play06:53

so i'm going to copy these routes out

play06:56

and i'm going to create

play06:59

a new file in lib called responses

play07:06

and what i'm going to do is i'm going to

play07:07

create two new functions one of them

play07:09

is going to be called html response

play07:16

and the other one will be not found

play07:28

response

play07:30

and i have to go back in here and get my

play07:33

imports and these both have to be

play07:37

exported

play07:41

and actually here i don't want to have

play07:43

index passed in

play07:44

i would like that to be passed in here

play07:46

as html

play07:48

and then i can get rid of the index here

play07:50

now i can head over to my

play07:53

index and import the responses from here

play07:57

so i'm going to say lib responses

play08:01

and i'm going to import the html

play08:02

response as well as my

play08:04

not found response then i can head down

play08:07

into the

play08:08

handle request into my routers and

play08:12

delete this and replace it with my html

play08:15

response with

play08:16

index and i'll do the same thing for the

play08:20

not found

play08:21

so i'll say not found a response

play08:24

and i don't need to pass it anything

play08:28

and now my routes look like this i also

play08:30

like the concept of

play08:31

pages so what i'm going to do also is

play08:34

inside of source i'm going to create a

play08:36

directory called pages

play08:38

and i'm going to create my index page

play08:41

here

play08:43

and what that is going to be is this

play08:46

route here

play08:49

so i'm just going to paste that in here

play08:50

so i'm going to say const

play08:52

index equals

play08:56

and i'm going to export default index

play09:01

and then i still have to import index

play09:03

from the top

play09:05

and because of the naming conflict i'll

play09:07

just call this index.html

play09:11

and next i'll do the same with the 404

play09:13

page

play09:14

so i'm going to create a new page called

play09:17

404.js

play09:20

and i'm just going to copy the same

play09:21

thing from index

play09:24

except here it's going to be 404

play09:27

i'm going to call this not found

play09:30

html

play09:35

and rename this to not found

play09:40

then i have to go into index and copy my

play09:43

not found response

play09:47

that doesn't take anything and i have to

play09:50

import

play09:51

those functions at the top too so

play09:54

i'm going to import the not found

play09:59

and then in index i need to import my

play10:03

html response

play10:07

and now that i have my pages set up i

play10:10

can go back into index

play10:12

so i'm going to close these out

play10:18

and i won't need these

play10:21

or the responses and i'm going to import

play10:25

index from pages index

play10:30

and down here in the get route for slash

play10:33

i'm just going to say index

play10:39

and for not found

play10:44

i'm going to do the same thing

play10:57

and let me just check one thing so

play11:00

because i move these into another level

play11:02

deep

play11:03

i'm going to have to change this to go a

play11:05

little bit

play11:06

deeper out

play11:10

and i probably have to do the same to

play11:12

responses

play11:15

and let's just double check that

play11:16

everything's still working so

play11:18

here it looks like the index is coming

play11:19

up correctly and

play11:21

my 404 is too so i think this is

play11:23

probably a good place to wrap up the

play11:25

video

play11:25

i really like what we have going on here

play11:28

we were able to import the html we have

play11:30

some pages

play11:31

and we have some custom routing as well

play11:34

and if you're curious how to deploy this

play11:36

onto the cloudflare workers platform be

play11:38

sure to check out my getting started

play11:39

video

play11:40

which i mentioned at the beginning of

play11:41

this as that goes into a little bit more

play11:43

detail

play11:44

and there's going to be more videos to

play11:45

come on cloudflare workers so be sure to

play11:48

subscribe

play11:49

and just as a reminder you got to click

play11:51

that bell icon if you want to receive

play11:52

notifications

play11:53

and everything we've covered in this

play11:55

video is going to be in an article that

play11:57

i'm going to

play11:58

link in the description below this will

play12:00

make it easier for you to follow along

play12:01

and copy and paste into your own project

play12:04

also check the description for links to

play12:06

the full source code

play12:07

if you've got questions about cloudflare

play12:09

workers or want to see something in an

play12:11

upcoming video

play12:12

be sure to post that in the comments

play12:14

below or hit me up on twitter

play12:16

and of course thanks for watching and

play12:18

i'll see you in the next video

play12:28

[Music]

play12:36

oh

play12:38

[Music]

play12:45

you

Rate This

5.0 / 5 (0 votes)

Related Tags
Cloudflare WorkerWeb ServerFree HostingHTML TemplatesRoutingWebpackWrangler CLIJavaScriptWeb DevelopmentTutorial Series