- 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
Created
March 21, 2021 10:37
-
-
Save celeroncoder/d14116a5e0aab6720639603a4ec891f6 to your computer and use it in GitHub Desktop.
Netlify Server Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetchData = async () => { | |
const result = await axios.get("/.netlify/functions/helloWorld"); | |
console.log(result.data.message); | |
// use data | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = async function (event, context) { | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
message: "Hi this is a change in the message 2", | |
}), | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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