How to build a REST API with TypeScript & MongoDB
Building a REST API with TypeScript and MongoDB can be a powerful combination for creating scalable and efficient web applications. In this article, we will guide you through the process of building a REST API using these technologies.
Setup
First, you will need to install Node.js and npm on your machine. Once you have Node.js and npm installed, you can create a new directory for your project and initialize it with npm.
mkdir rest-api
cd rest-api
npm init -y
Next, you will need to install TypeScript and Express by running the following commands:
npm install typescript @types/node @types/express ts-node
Creating the API
Now that your project is set up, you can start writing your API code. Create a new file called server.ts and add the following code:
import express, { Application, Request, Response } from 'express';
import mongoose from 'mongoose';
const app: Application = express();
const PORT: number = 3000;
app.get('/', (req: Request, res: Response) => {
res.send('Hello, World!');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
This code sets up a basic Express server and listens on port 3000. It also creates a simple route that responds with “Hello, World!”
Connecting to MongoDB
To connect to MongoDB, you will need to install the mongoose package and configure the connection in your server.ts file:
import mongoose, { ConnectOptions } from 'mongoose';
const MONGO_URI: string = 'mongodb://localhost:27017/rest-api';
const options: ConnectOptions = {
useNewUrlParser: true,
useUnifiedTopology: true
};
mongoose.connect(MONGO_URI, options)
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB: ', error);
});
With this code, you are now connected to your MongoDB database. You can now create models for your data and define routes for your API endpoints.
Conclusion
Building a REST API with TypeScript and MongoDB is a powerful combination that can help you create scalable and efficient web applications. By following the steps outlined in this article, you can start building your own REST API using these technologies.
One thing I love about the programming community is people just post absolute gems online for free. Props
I'm sorry Tom but I think your code needs a basic refactor. I'm generally a frontend developer, but based on your code, many things are wrong. I'm not sure if you are ready to do development video guidelines. 🙁 Also missing really important point and its request types validation you cant trust client requests if specific object property value has really string or number etc… In another hand its nice to see usage of mongoDb drivers instead of mongoose approach 🙂
Expectations: proper way of using TS with MongoDB
Reality: no real use of TS in this video 😐
So you made a typescript tutorial just to put everything as "any"?
hey Which keyboard are you using thanks man!
Well done! I watched you in full screen mode on my laptop and had no issues viewing your code. Hope to see more API dev / testing related content like this. Thank you for sharing!
Sorry, what MacBook are you using?
I came from ur instagram and ur doing an absolutly great job man in terms of aesthetics and all btw whats ur vs code theme name?
love this content. im new to mac so its good to see some of your development setup on your future videos.
You are impressive. How I've never found this account before I don't know, but I am glad to have found it now.
This video is useless if we can’t see the code
Start making course brother we will support you❤
Could not see anything on your laptop.
Whats the app for MongoDB?
U could increase the font