A robust and adaptable template for building serverless applications and static sites with Netlify CLI. This project offers a modern setup with TypeScript, multiple serverless functions, and a customizable static frontendโperfect for blogs, APIs, or dynamic web apps! โจ
- Serverless Functions: Four TypeScript-based functions (
hello,time,echo,neon) with robust error handling and CORS support. ๐ ๏ธ - Static Site: A simple, customizable HTML frontend in the
publicdirectory, ready for frameworks like React or Vue. ๐ - Netlify CLI Integration: Streamlined local development and deployment with
netlify dev. โ๏ธ - TypeScript Support: Type-safe functions using AWS Lambda types for reliability. ๐
- Yarn Workflow: Optimized package management with Yarn. ๐ฆ
- Modular Design: Easily extend with new functions, frontends, or integrations. ๐ง
- Neon-Ready: Prepared for Neon Postgres integration with the
neonfunction. ๐๏ธ
netlify-cli-template/
โโโ netlify/
โ โโโ functions/
โ โโโ hello.ts # Greets users with query params
โ โโโ time.ts # Returns current server time
โ โโโ echo.ts # Echoes POST request body
โ โโโ neon.ts # Placeholder for Neon Postgres integration
โโโ public/
โ โโโ index.html # Static site entry point
โโโ netlify.toml # Netlify configuration
โโโ package.json # Dependencies and scripts
โโโ tsconfig.json # TypeScript configuration
โโโ README.md # Project documentation
- Node.js: Version 14 or higher. ๐ข
- Yarn: Install with
npm install -g yarn. ๐ฆ - Netlify CLI: Install globally with
yarn global add netlify-clior locally viayarn add --dev netlify-cli. ๐ - Git: For version control and Netlify deployment. ๐
-
Clone the Repository:
git clone https://github.com/lizzyman04/netlify-cli-template.git cd netlify-cli-template -
Install Dependencies:
yarn install
-
Link to Netlify:
yarn netlify init
Follow the prompts to connect to your Netlify site or create a new one.
-
Local Development:
yarn start
Starts a local server at
http://localhost:8888. Functions are accessible athttp://localhost:8888/.netlify/functions/. -
Test Functions:
- hello:
GET /api/hello?name=YourName
Example: ReturnsHello, YourName! - time:
GET /api/time
Example: Returns current UTC time. - echo:
POST /api/echowith JSON body
Example: Echoes back the payload. - neon:
GET /extensions/neon
Example: Placeholder for Neon integration.
Example
curlforecho:curl -X POST http://localhost:8888/api/echo -d '{"test": "Hello"}' -H "Content-Type: application/json"
Example
curlforneon:curl http://localhost:8888/extensions/neon
- hello:
-
Deploy to Netlify:
yarn deploy
Or push to GitHub for continuous deployment. ๐
-
Type Checking:
yarn check-types
- Add Functions: Create new
.tsfiles innetlify/functions/. For custom folders (e.g.,netlify/extensions/), configurenetlify.tomlor use a build script. ๐ ๏ธ - Frontend: Swap
public/with frameworks like React, Vue.js, or Astro. ๐ - Environment Variables: Add to
.envor Netlifyโs UI (e.g.,NETLIFY_TOKEN,DATABASE_URLfor Neon). ๐ - Build Process: Modify
yarn buildfor static site generation. ๐๏ธ - Redirects: Update
netlify.tomlfor custom routing rules. ๐ค๏ธ
- hello: Greets users with a
namequery param (e.g.,/api/hello?name=Arlindo). ๐ - time: Returns current UTC time in multiple formats (e.g.,
/api/time). โฐ - echo: Echoes POST request body (e.g.,
POST /api/echo). ๐ข - neon: Placeholder for Neon Postgres integration (e.g.,
GET /extensions/neon). ๐๏ธ
The neon function is ready for Neon Postgres integration. To connect:
- Install the
pgpackage:yarn add pg. - Set
DATABASE_URLin.envor Netlifyโs UI with your Neon connection string. - Update
neon.tswith database logic (see commented example in the function). - Test queries locally and deploy. ๐
- Fork the repository. ๐ด
- Create a feature branch (
git checkout -b feature/your-feature). ๐ฟ - Commit changes (
git commit -m 'Add your feature'). ๐ - Push to the branch (
git push origin feature/your-feature). ๐ - Open a Pull Request. ๐
MIT License. See LICENSE for details. ๐
- Arlindo Abdul
- GitHub: Lizzyman ๐งโ๐ป
- Netlify CLI Docs ๐
- Netlify Functions Docs ๐
- TypeScript Docs ๐
- Neon Docs ๐
Built with โค๏ธ using Netlify CLI and Yarn. Happy coding! ๐