Skip to content

Instantly share code, notes, and snippets.

@celeroncoder
Created March 21, 2021 10:37
Show Gist options
  • Save celeroncoder/d14116a5e0aab6720639603a4ec891f6 to your computer and use it in GitHub Desktop.
Save celeroncoder/d14116a5e0aab6720639603a4ec891f6 to your computer and use it in GitHub Desktop.
Netlify Server Functions

Server Less Functions

Instructions

  • Create a netlify.toml file for using serverless functions on the website hosted on Netlify.
  • If any other hosting service search for the documentation for creating .toml file.
  • Put the functions in the functions directory in the root of the project.
  • Every function file must contain one export handler and only that will be valid and can be used.
    exports.handler = async function(event, context) {...};
  • Add statusCode field in the return object
    return {
      statusCode: code,
      body: JSON.stringify({...}),
    }
  • For any further refrence checkout this vedio on youtube by Ania Kubow
const fetchData = async () => {
const result = await axios.get("/.netlify/functions/helloWorld");
console.log(result.data.message);
// use data
};
exports.handler = async function (event, context) {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hi this is a change in the message 2",
}),
};
};
[build]
command = "npm run build"
functions = "functions"
publish = "build"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment