,

Creating a Node.js API with Express, TypeScript, PostgreSQL, and Drizzle ORM

Posted by






Building a Node.js API

Building a Node.js API

In this article, we will explore how to build a Node.js API using Express, TypeScript, PostgreSQL, and Drizzle ORM.

Setting up the project

First, let’s create a new directory for our project and initialize a new Node.js project with the following command:

      
        npx express-generator --view=pug my-api
        cd my-api
        npm install
      
    

Next, we’ll install the necessary packages for TypeScript and type definitions for Express:

      
        npm install typescript @types/node @types/express
      
    

We’ll also need to set up a PostgreSQL database and install the Drizzle ORM package:

      
        npm install pg drizzle-orm
      
    

Creating the API

Now that our project is set up, let’s create the API. We’ll start by creating a new file for our server using TypeScript:

      
        touch server.ts
      
    

Open the server.ts file and add the following code to set up a basic Express server:

      
        // server.ts
        import express, { Request, Response } from 'express';
        const app = express();
        const port = 3000;

        app.get('/', (req: Request, res: Response) => {
          res.send('Hello World!');
        });

        app.listen(port, () => {
          console.log(`Server is running on http://localhost:${port}`);
        });
      
    

Connecting to the database

Next, we’ll connect to our PostgreSQL database using Drizzle ORM. Create a new file for our database configuration:

      
        touch database.ts
      
    

Open the database.ts file and add the following code to set up the database connection:

      
        // database.ts
        import { Database } from 'drizzle-orm';
        const database = new Database({
          type: 'postgres',
          host: 'localhost',
          port: 5432,
          username: 'postgres',
          password: 'password',
          database: 'my-api-db',
        });
      
    

Building API endpoints

Now that we have our server and database set up, we can start building our API endpoints. Create a new file for our routes:

      
        touch routes.ts
      
    

Open the routes.ts file and add the following code to define our API endpoints:

      
        // routes.ts
        import express, { Request, Response } from 'express';
        const router = express.Router();

        router.get('/users', (req: Request, res: Response) => {
          // Get all users from the database
        });

        router.post('/users', (req: Request, res: Response) => {
          // Create a new user in the database
        });

        router.get('/users/:id', (req: Request, res: Response) => {
          // Get a specific user from the database
        });

        router.put('/users/:id', (req: Request, res: Response) => {
          // Update a specific user in the database
        });

        router.delete('/users/:id', (req: Request, res: Response) => {
          // Delete a specific user from the database
        });

        export default router;
      
    

Finally, we’ll import our routes into the server file and start the server:

      
        // server.ts
        import express from 'express';
        import routes from './routes';

        const app = express();
        const port = 3000;

        app.use('/api', routes);

        app.listen(port, () => {
          console.log(`Server is running on http://localhost:${port}`);
        });
      
    

With these steps, we have successfully built a Node.js API using Express, TypeScript, PostgreSQL, and Drizzle ORM. We now have a fully functioning API with database integration, and we can begin building our endpoints to handle different functionalities.


0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anurag Das
7 months ago

thanks man, this helped me a lot.

Ma rk
7 months ago

nice but it's complicated to set up and use the whole postgres thing