Many engineers think server-side code is the scary domain of True Engineers. Something newbies, juniors, and frontend engineers shouldn't touch.
They're wrong. Modern backend is a joy. ❤️
Why engineers are afraid of backend
Backend code scares people because it sounds complex. And serious, and critical, and a bit boring.
Here is a version that works
— Swizec Teller published ServerlessHandbook.dev (@Swizec) October 29, 2019
I really wish that after these 3 hours of hard work I had something better to show than "I am now reasonably certain that when I compare 2 numbers, the comparison is valid"
But that's not how backend life works. pic.twitter.com/t3FTcHo9Ai
Sometimes you fall into the Deep Backend and that's a dark place. You get your telemetry, a stream of data, a large database, and your job is to make sure they match at 99.999% consistency without dataloss.
You're fighting against unsolvable paradoxes, flakey servers, and the boredom of watching graphs wiggle around with no other feedback.
🤮
You *can* do backend logic
Most server-side code is not like that. It's JSON bureaucracy.
You get data from the client, change a few values, run a couple functions, and save it to the database. Trigger a background process or two to poke 3rd party APIs.
Easy peasy my friend. You do this on the frontend every day.
Doing it on the backend can be scary.
You have to set up a server, make sure it stays up, set up Nginx or Apache reverse proxies, configure a database, make sure that stays running, configure permissions, muck around with Docker, drop into Unix commands, set up monitoring, configure domains, set up a CDN for static files, deal with provisioning, configure backup servers, dynamic failover, horizontal scaling, vertical scaling, ....
Then you're ready to write your code.
Publish to reddit and your server dies under load 🤦♂️
Times have changed my friend
The future is already here, it's just not evenly distributed
~ William Gibson
You're standing one foot in the future, one foot in the past. We all are. How we build webapps is changing.
Serverless computing splashed on the scene with one promise: What if you didn't have to do all of the above? What if you could write your code and nothing else?
AWS Lambda launched in November 2014. Netlify in April 2015, Zeit/Vercel in December 2015.
All running towards the same goal: You write your code, we make it run.
No more funky configuration, no more servers, no more worrying about scale, no more setting up CDNs and deploy pipelines and parallel deploys and pull request previews and ... you write your code, the platform handles the rest.
What modern server-side code looks like
Here's what modern backend code looks like at its simplest:
// /api/helloWorld.jsmodule.exports = (req, res) => {return res.status(200).send("Hello world")}
module.exports
defines exports in Node, req
is the request object, res
the response. Send back a success status of 200 and a "Hello world"
text.
Deploy to Vercel with vercel
and you have a hello world function that runs on the server, has its own URL, and scales great. Try mine: https://hello-world-chi.vercel.app/api/helloWorld
With Netlify
You can use the same approach with Netlify. Their cloud functions look like this:
// /functions/helloWorld.jsexports.handler = async (event, context) => {return {statusCode: 200,body: "Hello world",}}
Export a handler
method, get a trigger event
instead of the req
object. Return your response instead of manipulating the res
object.
Both Vercel and Netlify run cloud functions on AWS Lambda. Vercel uses patterns from the popular ExpressJS framework for its abstraction, Netlify is like using AWS Lambda directly.
With AWS Lambda
To use AWS Lambda, you'd write the same JavaScript code and add a configuration file using Serverless Framework or CloudFormation
# serverless.ymlservice: hello-worldprovider:name: awsfunctions:helloWorld:handler: ./functions/helloWorldevents:- http:path: hellomethod: GET
Both use YAML for configuration. I prefer Serverless Framework because it makes common operations easy and lets you drop into full CloudFormation when you want.
This file says we're using aws
and creating a hello
function. Triggered by a GET request, handled by the /functions/helloWorld
file.
Hit AWS and you get infinite-ish scaling, an API Gateway as a reverse proxy, monitoring, distributed logging, and everything else you need. And the full config is part of your code. 😍
More work, more control, but not too much work 🤘
What do you think, sound cool? hit reply
Cheers,
~Swizec
PS: your code gets automatic SSL config in all 3 examples
Learned something new?
Want to become a high value JavaScript expert?
Here's how it works 👇
Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.
Start with an interactive cheatsheet 📖
Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.
"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.
Ready to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️