惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
量子位
S
Secure Thoughts
G
GRAHAM CLULEY
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 聂微东
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Engineering at Meta
Engineering at Meta
S
Security Affairs
Help Net Security
Help Net Security
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
博客园 - 叶小钗
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
H
Heimdal Security Blog
W
WeLiveSecurity
C
Check Point Blog

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode How to add testing to an existing project Profile a React App for Performance
Super simple start to Netlify functions
Netlify @Netlify · 2020-11-11 · via Kent C. Dodds Blog

Watch "Get started with Netlify Serverless Functions" on egghead.io

This last little while I've been doing a fair amount of work on kentcdodds.com. One page that I've been working on is the contact page where you can request enterprise training, schedule an hour of my time for consulting, or even leave your own testimonial of my work (I'd love it if you did!).

My app is built with GatsbyJS which has quickly become one of my favorite pieces of technology. Gatsby is a static site generator, so it generates a bunch of HTML, CSS, and JavaScript files in a directory called public in my project and it leaves it to me to get those files on the World Wide Web for you and your loved ones to enjoy.

So how do I get those files up there on the internet? I use Netlify! I've LOVED Netlify for years. It's bonkers how quickly I can get a site up and running (with HTTPS) with Netlify and it's integration with GitHub is unmatched. I love it.

Great, so now I've got static files served ultra-fast on the internet with Netlify's CDN. But how do I make it so my contact form can actually send emails? You may not be aware of this, but web browsers are not able to send emails themselves. To do that, you need a server. I don't know about you, but managing servers and databases are among the top things on the list of stuff I do NOT enjoy about web development.

You may have heard of "Serverless" or "Lambda Functions" or "Azure Functions" etc. What's cool about this is you can write your server code without worrying about managing servers or paying to have them sit around waiting for requests all day long (which is particularly useful for a contact form like mine).

I remember years ago when those started getting popular, I tried them out a bit and was reminded why I don't enjoy working with backend/devops stuff. As cool as these functions were conceptually, I just didn't feel like I was smart enough to navigate the complexities of the offerings.

Luckily for us, almost exactly one year ago from the time I'm writing this, Netlify took the complexity of AWS Lambda functions and made it super simple (I think it's what they do best):

I want to give you a really quick intro to using Netlify to deploy server code in a way that is mind-blowingly straightforward to me.

How to use Netlify Lambda Functions

Before you get started, you'll need a GitHub account, a Netlify account, and a fresh repository. Once you've got the project locally on your computer, then you can start with the following steps:

Step 1: Create a netlify.toml file

This file configures some stuff about our repository for netlify. Here's all you need for this to work:

[build]
  functions = "./functions"

All this is saying is: Hey Netlify, when you build my project, the functions are located in the "functions" directory.

Step 2: Create functions/hello.js

Now that we've told Netlify where to find our functions files, let's make one:

// functions/hello.js
exports.handler = async (event) => {
	const subject = event.queryStringParameters.name || 'World'
	return {
		statusCode: 200,
		body: `Hello ${subject}!`,
	}
}

Netlify uses AWS Lambda under the hood, so you can checkout documentation and learning materials about creating AWS Lambda functions for what kind of code can go in there. We'll start with something simple that returns a promise (note it's an async function) which resolves to an object for the response we want for this function. You can do all kinds of things in that function (like connect with an SMTP mail server and send an email which is what I do in my contact form).

Step 3: Commit and push

The above code in an editor

Now you can just do: git add -A, then git commit -m 'init', and git push -u origin master and you're set! Now let's go over to Netlify.

The github repository

Step 4: Setup Netlify

Go to your netlify sites and click "New site from Git." From there you'll connect your Netlify account to your GitHub account and it'll show you a list of repositories. Search for the one you want and click on it.

Create New Site page

Go ahead and leave the "Build command" and "Publish directory" fields empty and ignore the advanced options. Just click "Deploy site."

Overview page

Now you're on the Overview page of your site. It should only take a few seconds to deploy your site. You can see info about that on the "Deploys" page. While you wait, you can edit the Site name to be something more memorable by clicking "Site settings."

Once the deploy is finished, you can go to the "Functions" page:

Functions page showing hello.js

There you should see hello.js! If you click that it'll show you the function log. Calls from your function to console.log should appear here with a timestamp as well as a note for whenever your function is invoked:

Function log page

Checkout the "Endpoint" and it shows your function is available on the World Wide Web at https://<your-site-name>.netlify.com/.netlify/functions/hello.

Really quick I just want to explain something. In netlify.toml we told Netlify that it can find the JavaScript for our functions in the ./functions directory (we could've called that whatever we wanted). Netlify takes each JavaScript file in there and deploys it (as-is) to AWS lambda, then sets up an endpoint for hitting that function for us under /.netlify/functions (this is not configurable AFAIK).

Cool, so now we can hit that URL and boom, Hello Bob Ross!

Function output saying Hello Bob Ross

Wahoo!

Next steps

You can find my version of this project on GitHub and on Netlify.

One important thing to note is that Netlify will send only the files for our functions to AWS and no more. This means that if you try to require something from a local utils directory or node_modules you're out of luck.

Also, testing this locally is... not easy...

But wait! Netlify engineers have built netlify-lambda to solve both of these problems! I've taken the liberty of giving you an example of this same function that uses netlify-lambda here on GitHub.

Here are the main changes:

  1. Added a package.json to configure npm/yarn to install netlify-lambda and created dev and build scripts.
  2. Added a .gitignore to ignore the .netlify directory which is where netlify-lambda will place our compiled functions.
  3. Added a command line in our netlify.toml file to have the build run the command npm run build which will trigger netlify-lambda to build our function into a single file for delivery to AWS Lambda.
  4. Updated the functions line in our netlify.toml file to point to .netlify/functions/ which is where netlify-lambda will place our final built functions.

Everything else is the same, but now we can actually require different files!

Conclusion

Any time we need to make a change to our function, we simply make those changes, commit them to GitHub and Netlify will automatically update that on AWS Lambda. It's very fast and super empowering.

I love enabling technologies like this. I shouldn't have to build an entire backend with a database and server farm to allow you to send me Enterprise Training Inquiries, and thanks to Netlify, I don't have to. It's pretty slick!

Give it a try! I think you'll love it too.

P.S.

Also, Shawn mentioned that Netlify does have a Forms service built-in for contact forms like I have. I originally used that, but it doesn't actually send emails (you have to integrate with Zapier and it's an ok experience).

Also, while /.netlify/functions is not configurable, you can alias it to anything with a redirect, including serving your entire site from a function which is pretty legit.