Orchestrating Node.js, Next.js, and Prisma for Full-Stack Mastery
In this article, we shall see how we can Integrate Frontend(Next.js), Backend(Node.js) and ORM(Prisma).
Assembling a full-stack application requires harmonizing the backend prowess of Node.js, the reactive magic of Next.js on the frontend, and the robust data management capabilities offered by Prisma. This guide is your compass through the intricate steps of this integration, generously providing code snippets and detailed explanations at each juncture.
Prerequisites:
Node.js and npm: Ensure the presence of Node.js and npm on your machine. If not, procure them from the source of all things Node at nodejs.org.
Next.js: Elevate your project with the global installation of Next.js:
npm install -g create-next-app
Prisma: Grant Prisma global access as well:
npm install -g @prisma/cli
Step 1: Cultivate the Node.js Backend Garden
Sow the seeds of a new Node.js project:
mkdir backend && cd backend
npm init -y
Nourish your project with essential packages:
npm install express prisma
Cultivate the index.js
file for your Node.js server:
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const app = express();
const prisma = new PrismaClient();
app.get('/api/users', async (req, res) => {
const users = await prisma.user.findMany();
res.json(users);
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server is flourishing on http://localhost:${PORT}`);
});
Step 2: Lay Down Prisma Roots for Database Integration
Initiate Prisma's roots in your project:
npx prisma init
Craft your database model in the soil of the prisma/schema.prisma
file:
// prisma/schema.prisma
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
Sow the seeds of change into your database:
npx prisma migrate dev --name init
Infuse life into your database with sample data:
npx prisma db seed --preview-feature
Step 3: Cultivate the Next.js Frontend Oasis
Cultivate a lush Next.js garden:
npx create-next-app frontend
cd frontend
Water your garden with essential packages:
npm install swr axios
Create a vibrant component to fetch and display user data:
// frontend/components/UserList.js
import useSWR from 'swr';
import axios from 'axios';
const fetcher = (url) => axios.get(url).then((res) => res.data);
export default function UserList() {
const { data: users, error } = useSWR('/api/users', fetcher);
if (error) return <div>The garden is wilting - error loading users</div>;
if (!users) return <div>The garden is blossoming - loading...</div>;
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Step 4: Intertwine Frontend and Backend Ecosystems
Tie the roots of your frontend with the branches of your backend. Update the frontend/pages/index.js
file to include the UserList
component:
import UserList from '../components/UserList';
export default function Home() {
return (
<div>
<h1>Inhabitants of the Oasis</h1>
<UserList />
</div>
);
}
Step 5: Witness the Symphony in Action
Run the backend server:
cd ../backend
node index.js
Run the Next.js app:
cd ../frontend
npm run dev
Behold the culmination of your orchestration at http://localhost:3000. Witness a harmonious display of users fetched seamlessly from the backend, a testament to the synchronized dance of Node.js, Next.js, and Prisma.
This comprehensive guide acts as your maestro's wand, orchestrating the symphony of Node.js, Next.js, and Prisma into a delightful full-stack concerto. Feel free to embellish and extend this foundation to suit the unique requirements of your project. Happy coding, and may your application flourish!