/clean-express-prisma

Clean Express Boilerplate including Prisma, Typescript, Prettier & File Routing

Primary LanguageTypeScript

clean-express-prisma is a production-ready Express boilerplate including a fresh setup of Prisma, Typescript, Prettier, Heroku and file-based routing from Express File Routing.

Features

  • File-based routing
  • Preconfigured path aliases
  • Security middlewares for production
  • Cleanest possible project structure

Getting Started

Clone the repository or Use this template.

git clone https://github.com/matthiaaas/clean-electron-react.git YOUR-PROJECT-NAME
cd YOUR-PROJECT-NAME
yarn

Configuration

Prisma Database Setup

Create a (PostgreSQL) database and add it's url to .env.

DATABASE_URL=postgresql://...

Note: See Prisma Docs and PostgreSQL Installation Guide (macOS) on how to get started.

Development

Start a dev server on localhost:4000.

yarn dev

Production

The template contains pre-configured Procfile and is ready to be deployed on Heroku out-of-the-box. For deployment on other platforms further configuration might be necessary.

Deploy to Heroku

(Not Recommended) Manually run build scripts for production

yarn build
yarn db:deploy
yarn start

File-based routing

Files inside your project's /routes directory will get matched an url path automatically.

├── app.ts
├── routes
    ├── index.ts // index routes
    ├── posts
        ├── index.ts
        └── :id.ts // dynamic params
    └── users.ts
└── package.json
  • /routes/index.ts → /
  • /routes/posts/index.ts → /posts
  • /routes/posts/:id.ts → /posts/:id
  • /routes/users.ts → /users

Note: See express-file-routing for more details on how to use file-based routing.

Route Example

  • /routes/projects.ts
import prisma from "~/lib/prisma"

export const get = async (req, res) => {
  const projects = await prisma.projects.findMany()

  return res.json(projects)
}

export const post = async (req, res) => {
  const { name } = req.body

  const project = await prisma.project.create({
    data: {
      name
    }
  })

  return res.json(project)
}