0.0.2 ⢠Published 4 years ago
carpincho v0.0.2
carpincho š§
The express framework.
Build backend applications with zero configuration, typescript support, and schema validations in seconds. Inspired in NextJs ā„
Getting started
- Create a folder for your project and install carpincho
npm i carpincho- Add scripts to your package.json
"scripts": {
"start": "node dist/index.js",
"build": "carpincho"
}- Create a folder named
routesand inside, add a fileindex.api.ts:
// routes/index.api.ts
export const GET = (req, res) => {
res.send('Hello world');
};- Thats it, run
npm run buildto build your express api andnpm run startto start the server. - Open
localhost:3000to see your api in action š
Route parameters
Use $id in folders or files to define paraeters for the urls.
// File /routes/users/$userId.api.ts
// URL http://localhost:3000/users/42
export const GET = (req, res) => {
req.params; // { userId: '42' }
res.json(req.params);
};// File /routes/users/$userId/books/$bookId.api.ts
// URL http://localhost:3000/users/42/posts/101
export const GET = (req, res) => {
req.params; // { userId: '42', bookId: '101' }
res.json(req.params);
};Data validation
š§ PENDING FEATURE
import { Joi } from 'carpincho';
export const POST = (req, res) => {
res.send('Hello world');
};
POST.schema = {
body: {
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().required(),
},
};Using Typescript
To start using typescript in an existing project, create an empty tsconfig.json file in the root folder:
touch tsconfig.jsonCarpincho will configure this file with default values. Or you can edit it to your needs.
Typed requests and responses
// File /routes/users/$userId/books/$bookId.api.ts
// URL http://localhost:3000/users/42/posts/101
import { Request, Response } from 'carpincho';
interface Params {
userId: string;
bookId: string;
}
export const GET = (req: Request<Params>, res: Response) => {
req.params; // { userId: '42', bookId: '101' }
res.json(req.params);
};Environment Variables
Create a .env file with your environment variables and they will be loaded into process.env.
Example .env:
# .env
PORT=3000š§
