AWS Lambdas: Vercel With Next.js
Project Init
npx create-next-app vercel-with-next-js && cd $_
1. Log Hello World
npm run dev
⚠️ zsh: command not found: next
|
Use npm run dev instead of next dev
|
2. Deploy Hello World
3. Pass URL Params
vercel-with-next-js > pages > api > hello.js
export default (req, res) => {
const name = req.query.name ?? "World"
res.status(200).json({ body: `Hello ${name}` })
}
vercel-with-next-js > pages > api > hello > [name].js
export default function handler(req, res) {
const { name } = req.query
res.end(`Hello ${name} (v2)`)
}
4. Install NPM Packages
npm i @astrajs/collections
5. Set Env Values
// pages/api/hello.js
export default async (req, res) => {
const region = process.env.ASTRA_DB_REGION
const name = req.query.name ?? "World"
res.status(200).json({
body: `Hello ${name}. Region is ${region}.`
})
}
6. Test Token
const { createClient } = require("@astrajs/collections");
export default async (req, res) => {
const region = process.env.ASTRA_DB_REGION
const name = req.query.name ?? "World"
// 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,
});
res.status(200).json({
body: `Hello ${name}.
Region: ${region}.
Token: ${astraClient.restClient.applicationToken}.`
})
}
7. Deploy Authenticated