Node.
js, Express
You’re viewing user-generated content that may be
unverified or unsafe.
Report
Edit with ChatGPTEdit
Setting Up a Node.js, Express.js, Prisma,
and PostgreSQL Project
1. Prerequisites
Make sure you have the following installed:
Node.js (check with node -v)
npm (comes with Node.js, check with npm -
v)
PostgreSQL (check with psql --version)
2. Project Setup
Step 1: Create a new Node.js project
mkdir myapp
cd myapp
npm init -y
What it does: Creates a new directory and
initializes a Node.js project with a
package.json file.
3. Install Dependencies
Step 2: Install required packages
npm install express prisma @prisma/cl
npm install -D nodemon
express: Web framework for building APIs.
prisma: ORM for database interactions.
@prisma/client: Prisma's client library to
interact with the database.
dotenv: Loads environment variables from a
.env file.
nodemon: Automatically restarts the server on
changes (development only).
4. Initialize Prisma
Step 3: Initialize Prisma
npx prisma init
What it does: Creates a prisma folder with a
schema.prisma file.
Step 4: Set up your database URL
Add your PostgreSQL connection string in the .env
file:
DATABASE_URL="postgresql://username:p
5. Define Your Data Model
Step 5: Edit the prisma/schema.prisma file
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(aut
name String
email String @unique
createdAt DateTime @default(now())
}
What it does: Defines a User model with an
auto-incrementing ID, name, unique email, and
timestamp.
6. Migrate Your Database
Step 6: Run migrations
npx prisma migrate dev --name init
What it does: Creates a migration file and
applies it to the database.
Step 7: Generate the Prisma client
npx prisma generate
What it does: Generates the client library to
interact with your database.
7. Create Your Server
Step 8: Create the server file
Create an index.js file:
const express = require('express');
const { PrismaClient } = require('@pr
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const prisma = new PrismaClient();
app.use(express.json());
app.get('/users', async (req, res) =>
const users = await prisma.user.fin
res.json(users);
});
app.post('/users', async (req, res) =
const { name, email } = req.body;
try {
const newUser = await prisma.user
res.json(newUser);
} catch (err) {
res.status(400).json({ error: err
}
});
const PORT = process.env.PORT || 3000
app.listen(PORT, () => console.log(`S
8. Add Scripts to package.json
Step 9: Update scripts
"scripts": {
"dev": "nodemon index.js"
}
What it does: Adds a script to run the server
using nodemon.
9. Run Your Server
Step 10: Start the server
npm run dev
10. Test Your API
Step 11: Test with curl or Postman
curl http://localhost:3000/users
Add a user:
curl -X POST http://localhost:3000/us
11. Stop the Server
Press Ctrl + C to stop the server.
Recap
1. Initialize the project.
2. Install dependencies.
3. Initialize Prisma and set up the database.
4. Define data models.
5. Run migrations.
6. Set up the server.
7. Add scripts for server running.
8. Start the server.
9. Test the API.