Our great sponsors
-
netlify-serverless-demo
Repo for demo app that builds a serverless Express.js API using Netlify Functions
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
The code for this demo can be found here. Since the purpose of this post is to show how to set up a serverless API, rather than do anything specific with the API, this particular demo application will serve up an extremely basic API with two endpoints that don't do much. I will assume the reader has some basic familiarity with Express.js and Node as we will be using these to build the API.
Lastly, on line 29, we will utilize the serverless-http package to wrap our application up for serverless use. This means our application can work as expected without any HTTP server, ports, or sockets.
The start script is fairly straight forward, it will just execute our server-local.js file which in turn is calling express/server.js. This works for local development, but we need to do additional work in our build script for the application to work as a serverless function once deployed to Netlify. In the build script, we call netlify-lambda build which takes a source folder as an argument (express in our case) and outputs it to a built folder. The built folder is where Netlify will look for our serverless functions. We have a couple of options for how we specify this. We could specify the designated folder within Netlify's application settings using Netlify's web app, or within our application code, we can specify the designated folder with a netlify.toml configuration file. In our case, we'll use a configuration file that lives in our root directory that looks like this:
Related posts
- List of people offering their time for free to have a "coffee chat"
- Getting Started with Next.js: Part 1 - Setting Up Your Project
- UX Case Study: Markdown Heading
- System & Database Design (Day 1) - Creating a SaaS Startup in 30 Days
- Como foi fazer um decodificador do zero para o projeto final da 1ยช parte do OracleONE