Implement CRUD APIs using Typescript and Node JS

Tudip Technologies
2 min readJan 6, 2022

If you want to create CRUD APIs using typescript and node js. You are at the right place. Here you will learn to write APIs from scratch.

Typescript is a superset of javascript and it supports static typing. For large Javascript project, adopting Typescript may result in robust software while still being deployable where a regular javascript application run.

Follow the below steps and get your CRUD APIs ready :

Step 1: Initialize your project:

  • Hit npm init on your terminal

It will create package.json file in your project

Step 2: Install necessary packages:

  • npm install typescript express ts-node

Why are these 3 packages required?

We will have to create and execute typescript classes. For execution of typescript classes we would need a typescript execution engine. ts-node is a typescript execution engine. Its JIT transforms Typescript into Javascript, enabling you to directly execute typescript on Node.js without precompiling

To use typescript with Node we use this package. It helps building typescript project

Express is an application framework which we used to develop REST APIs which provides a simple routing for requests made by clients

Step 3: Create app.ts file:

import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as mongoose from 'mongoose';
import AppRoutes from './routes/index';
import DevConfig from './config/environments/development';
class App {
public app: express.Application;
public port: number;
public config = new DevConfig();
appRoutes = new AppRoutes()

constructor(controllers, port) {
this.app = express();
this.port = port;

this.initializeMiddlewares();
this.initializeRouters(this.appRoutes.routers);
}

private initializeMiddlewares() {
this.app.use(bodyParser.json());
}
// Initialize all the routes of the application
private initializeRouters(router) {
router.forEach(routes => {
this.app.use('/', routes);
});
}

// Server will listen to this port
public listen() {
this.app.listen(this.port, () => {
console.log(`App listening on the port ${this.port}`);
});
}
// Connecting to mongo DB
public connectToTheDatabase() {
console.log("Connecting to mongo DB", this.config.dbURL);
mongoose.connect(this.config.dbURL);
}
}

export default App;
  • app.ts: It is a class that does the following things:
  • Calls middleware
  • Connects to MongoDB
  • Starts the server
  • Initialize the routes

So whenever we want to use any middleware in our application. We will have to import it and use it inside the initializeMiddleware function in the app.ts file

Read more:https://tudip.com/blog-post/implement-crud-apis-using-typescript-and-node-js/

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response