Building a robust CRUD app requires a solid foundation, and the first step is setting up your development environment. In this post, we will walk through the steps to configure your system for a Node.js, MongoDB, and TypeScript project. By following these steps, you will be able to quickly start coding and focus on what matters most—creating an amazing application.
Before diving into the setup, ensure you have the following prerequisites installed on your system:
To check if Node.js and npm are installed correctly, open your terminal (Command Prompt for Windows, Terminal for macOS and Linux) and run:
node -v npm -v
You should see the installed versions of Node.js and npm displayed.
If you prefer to install MongoDB locally, follow these steps:
Visit the MongoDB Download Center and download the installer appropriate for your operating system.
Follow the installation wizard. On Windows, you can opt for a complete installation.
After installation, start the MongoDB server by running:
mongod
This will run the MongoDB server and create a local database instance that can be accessed from your application.
If you prefer MongoDB Atlas:
Now that we have Node.js, npm, and MongoDB set up, let’s create a new folder for your project. Open your terminal and navigate to your desired location, then create your project directory:
mkdir my-crud-app cd my-crud-app
To initialize your Node.js project, run:
npm init -y
This will create a package.json
file in your project folder, which will manage your project dependencies and settings.
To use TypeScript in your project, install it globally:
npm install -g typescript
Next, you’ll need to set it up in your project. Run the following command to create a tsconfig.json
file:
tsc --init
This file allows you to define various options for the TypeScript compiler, such as target version and module resolution.
Express.js is a popular web application framework for Node.js that simplifies the development process. To install Express, run:
npm install express
You may also want to install the types for Express, which provides TypeScript definitions. Run:
npm install --save-dev @types/express
Mongoose is a powerful library that provides a straightforward way to interact with MongoDB from Node.js. To install Mongoose, run:
npm install mongoose npm install --save-dev @types/mongoose
Your project structure might look like this:
my-crud-app/
├── node_modules/
├── src/
│ ├── controllers/
│ ├── models/
│ ├── routes/
│ ├── app.ts
├── package.json
├── tsconfig.json
Now, let’s create the app.ts
file in the src
directory. This will be the starting point for your CRUD app. Open your code editor and create the file:
// src/app.ts import express from 'express'; import mongoose from 'mongoose'; const app = express(); const PORT = process.env.PORT || 5000; // Middleware to parse JSON app.use(express.json()); // Connect to MongoDB mongoose.connect('mongodb://localhost:27017/mycrudapp', { useNewUrlParser: true, useUnifiedTopology: true, }) .then(() => console.log('MongoDB connected')) .catch(err => console.log('MongoDB connection error:', err)); // Start the server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
Replace the connection string with your MongoDB Atlas connection string if you're using Atlas.
To run your TypeScript application, you can create a script in your package.json
:
"scripts": { "start": "tsc && node dist/app.js" }
In this script, we first compile the TypeScript files, then run the resulting JavaScript file. To run your application, use this command:
npm start
If everything is set up correctly, you should see Server is running on http://localhost:5000
printed in your terminal.
This setup will provide a solid foundation for your CRUD app using Node.js, MongoDB, and TypeScript. With these tools and structures in place, you’re ready to start building out your application's functionality!
31/08/2024 | NodeJS
14/10/2024 | NodeJS
08/10/2024 | NodeJS
14/10/2024 | NodeJS
14/10/2024 | NodeJS
23/07/2024 | NodeJS
14/10/2024 | NodeJS
31/08/2024 | NodeJS
18/09/2024 | NodeJS
14/10/2024 | NodeJS