DEV Community

Greg, The JavaScript Whisperer
Greg, The JavaScript Whisperer

Posted on • Originally published at gregbenner.life

2 1

Caddy 2 server the silver bullet of serving

Caddy 2 server the silver bullet of serving

First and foremost this post is about a Go-lang server; wait it will be in a node.js context!

Node.js is beautiful and brilliant. It takes a mature developer to admit where something shines and where it could use some help. With every language, or library the authors make some choices on where it should it shine and what can lag.

I won't drill down into the nitty gritty but where I think node can use a friend like Caddy are things like serving binary files, rate limiting access to the server, creating an HTTP/2 or /3 server. (node recently made HTTP/2 a first class citizen but you still need to setup a Let's encrypt certificate)

HTTP/2 really shines with serving static assets, in the past with HTTP/1 we used CDN's to maximize parallel requests and dns lookups. Nowadays you can create many requests with only one DNS lookup and all the assets over the same wire.

I've been using Caddy since the early v1 days, there are a few feature I miss that haven't landed in v2 yet; such as protecting routes with JWT via a plugin. Overall if you are creating a new project you should use V2.

Getting started!
You can download the binary here or use the handy DigitalOcean droplet

For me a common use case of Caddy is as a static file server and a reverse proxy for my backend services. Here's what works for me as "Caddyfile" the yaml edition of the configuration file

example.com {
encode gzip
header Cache-Control max-age=31536000
file_server {
root /var/www/html
}
log {
output file /var/log/access.log
}

}

api.example.com {
reverse_proxy 127.0.0.1:3000
}
Enter fullscreen mode Exit fullscreen mode

And just like that you are likely to get 100 lighthouse score on google served over http/2.

Here's more information about Caddy, hope this helps!

Comments and retweets welcome https://twitter.com/cactusanddove/status/1274861623094763531?s=20

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay