An example project using the examples from the netlify functions docs.
- Node v18.19+ (see
.nvmrc
file)
$> yarn
The functions can be run locally via `npx netlify dev` but if you want to deploy it on netlify, then you need to link
your netlify account with your own version of this repo in Github and use `npx netlify link` once you have a site setup.
$> npx netlify dev
◈ Netlify Dev ◈
◈ Ignored general context env var: LANG (defined in process)
◈ No app server detected. Using simple static server
◈ Unable to determine public folder to serve files from. Using current working directory
◈ Setup a netlify.toml file with a [dev] section to specify your dev server settings.
◈ See docs at: https://cli.netlify.com/netlify-dev#project-detection
◈ Running static server from "netlify-functions"
◈ Setting up local development server
◈ Static server listening to 3999
┌─────────────────────────────────────────────────┐
│ │
│ ◈ Server now ready on http://localhost:8888 │
│ │
└─────────────────────────────────────────────────┘
⠋ Setting up the Edge Functions environment. This may take a couple of minutes.◈ Loaded function hello
✔ Setting up the Edge Functions environment. This may take a couple of minutes.
Request from ::1: GET /.netlify/functions/hello
Response with status 200 in 479 ms.
Open http://localhost:8888/.netlify/functions/hello
in your browser to hit the hello endpoint.
These functions use the .mts
extension which allows use of ECMA modules in typescript without specifying the package
to be a module
(via package.json
). If .ts
is preferred, then rename to .ts
and add "type": "module"
to the
package.json
file.
Functions are defined within the netlify-functions
directory. Once run using npx netlify dev
these functions are
available via the http://localhost:8888/.netlify/functions/<functionName>
url.
A simple hello world function.
http://localhost:8888/.netlify/functions/hello
A synchronous function lets you implement a traditional client/server interaction, where the connection is kept open until the function execution is finished, allowing the client to wait for a response before rendering a page or moving on to the next task.
This examples returns a content every second up to a max of 5 seconds.
http://localhost:8888/.netlify/functions/synchronous
Instead of using the function name as the endpoint, netlify functions can be configured to use a specific path e.g
"/travel-guide/:city/:country"