Deploy your node application using netlfiy functions
AWS’s serverless Lambda functions open a world of possibilities for running on-demand, server-side code without having to run a dedicated server.
Netlify lets you deploy serverless Lambda functions without an AWS account, and with function management handled directly within Netlify. Your serverless functions are version-controlled, built, and deployed along with the rest of your Netlify site, and we will automatically handle service discovery through our built-in API gateway. This eliminates overhead and brings the power of Deploy Previews and rollbacks to your serverless functions.
We will run through how to deploy a ‘Hello World’ express.js app to netlify.
Create project
To start, create a new directory and cd into it:
mkdir netlify-demo
cd netlify-demo
Create package.json
Next make a package.json file with all the default values. You can do this via yarn init
and simply answering the questions that follow via the terminal.
Add the netlify-lambda package
The netlify-lambda package will serve our functions locally, and then compile for production.
yarn add express netlify-lambda serverless-http
Create a .gitignore
file:
node_modules
functions
Set up netlify depolyment files
Create a netlify.toml
file:
[build]functions = "functions"
Every time you deploy to netlify, you need to give it a dist
directory:
The index.html
file can be blank.
Next, create a src
directory and api file:
Now add these test endpoints:
We can set up the following script commands in package.json
:
"scripts": {
"start": "./node_modules/.bin/netlify-lambda serve src",
"build": "./node_modules/.bin/netlify-lambda build src"
},
You should be able to run the app with yarn start
and hit these endpoints with http://localhost:9000/.netlify/functions/api/test
Finally, to deploy this to netlify, add the project to a git repository and connect it with netlify.
Thanks for reading!