npm install netlify-cli -g
📖 Official Netlify Docs: Installation |
---|
npx create-react-app netlify-with-create-react-app && cd $_
1. Log Hello World
netlify init
netlify functions:create hello-world
netlify dev
|
---|
killall node
|
📖 Official Netlify Docs: Get started with Netlify Dev |
---|
📖 Official Netlify Docs: Build serverless functions with JavaScript |
---|
📖 Official Netlify Docs: Playground: Hello, World! |
---|
📖 Official Netlify Docs: functions:create |
---|
2. Deploy Hello World
netlify deploy
› Did you forget to run a build? |
---|
Set your Directory to Deploy as blank or "." during init, or through the web dashboard in Site Settings: Build & Deploy. |
📖 Official Netlify Docs: Configure and deploy Functions |
---|
3. Pass URL Params
exports.handler = async (event, context) => {
const name = event.queryStringParameters.name || "World";
return {
statusCode: 200,
body: `Hello ${name}`,
};
};
📖 Official Netlify Docs: Playground: Hello, {name} |
---|
5. Set Env Values
const handler = async (event) => {
try {
const region = process.env.ASTRA_DB_REGION
const subject = event.queryStringParameters.name || 'World'
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello, ${subject}. Region: ${region}` }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }
}
}
netlify dev
📖 Official Netlify Docs: Environmental Variables |
---|
6. Test Token
const { createClient } = require("@astrajs/collections");
const handler = async (event) => {
try {
const region = process.env.ASTRA_DB_REGION
// create an Astra client
const astraClient = await createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
});
const subject = event.queryStringParameters.name || 'World'
return {
statusCode: 200,
body: JSON.stringify({
message: `Hello, ${subject}.
Region: ${region}.
Astra Token: ${astraClient.restClient.applicationToken}.` }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }
}
}
module.exports = { handler }