Are you looking to dive into the world of backend web development? Express.js might just be the perfect starting point for you. As one of the most popular and widely used frameworks for Node.js, Express.js provides a robust set of features for building web applications and APIs with ease. In this guide, we’ll walk through the basics of setting up an Express.js application and integrating database connectivity using Prisma, a modern database toolkit.
What is Express.js?
Express.js is a minimalist web framework for Node.js, designed to make building web applications and APIs straightforward and efficient. With its simple and flexible routing system, middleware support, and a vibrant ecosystem of plugins, Express.js empowers developers to create scalable and powerful server-side applications.
Setting Up Your Project
Before we dive into coding, let’s make sure we have Node.js and npm (Node Package Manager) installed on our system. Once that’s done, we can create a new directory for our project and initialize a new Node.js project by running the following commands in your terminal:
mkdir express-prisma-example
cd express-prisma-example
npm init -y
Next, let’s install Express.js and Prisma as dependencies:
npm install express prisma
Creating an Express.js Application
Now that we have our project set up, let’s create a basic Express.js application. Create a new file called app.js
in your project directory and add the following code:
const express = require('express');
const app = express();
// Define a route
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
This code sets up a simple Express.js server that listens for incoming HTTP requests on port 3000 and responds with “Hello, Express!” when a GET request is made to the root route (“/”).
Integrating Prisma for Database Connectivity
Now, let’s add database connectivity to our Express.js application using Prisma. For this example, we’ll use SQLite as our database engine. First, let’s install the required Prisma dependencies:
npm install @prisma/cli @prisma/client
Next, we’ll initialize Prisma in our project by running the following command:
npx prisma init
This command will create a new directory called prisma
in our project, containing the Prisma schema file (schema.prisma
) and a .env
file for configuring our database connection.
Defining the Prisma Schema
Open the schema.prisma
file in your project directory and define your database schema. For example, let’s create a simple schema for storing users:
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
This schema defines a User
model with id
, name
, and email
fields.
Using Prisma in Your Express.js Application
Now that we have our Prisma schema defined, let’s use Prisma in our Express.js application to perform database operations. Update your app.js
file with the following code:
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const app = express();
app.use(express.json());
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany();
res.json(users);
});
app.post('/user', async (req, res) => {
const { name, email } = req.body;
const newUser = await prisma.user.create({
data: {
name,
email,
},
});
res.json(newUser);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
In this updated code, we’ve imported the PrismaClient
from @prisma/client
and created a new instance of PrismaClient
. We’ve also added two new routes: /users
to fetch all users from the database, and /user
to create a new user.
Conclusion
You’ve successfully set up an Express.js application and integrated database connectivity using Prisma. This is just the beginning of your journey into backend web development with Node.js and Express.js. From here, you can explore additional features and functionalities offered by Express.js and Prisma to build even more powerful and dynamic web applications.