Skip to main content

Coding1 -πŸ“˜ Creating the Project Folder Structure

🧠 What You’ll Learn​

In this lesson, you’ll set up the folder structure for your NodeJS RESTful API using:

  • Node.js + Express
  • TypeScript
  • MongoDB (with Mongoose)

🧱 Step 1: Initialize a New Project​

Open your terminal and run:

mkdir REST-API
cd REST-API
npm init -y

This creates your project folder and a basic package.json file.


βš™οΈ Step 2: Install Required Dependencies​

Install these packages:

npm install express mongoose dotenv
npm install --save-dev typescript ts-node-dev @types/node @types/express @types/mongoose

πŸ› οΈ Step 3: Initialize TypeScript​

Run:

npx tsc --init

Then update your tsconfig.json for better development experience:

{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}

πŸ“ Step 4: Create Your Folder Structure​

Inside the root directory, create a folder named src:

mkdir src
cd src
mkdir controller model routes
touch app.ts dbconnect.ts

Your final folder structure should look like this:

REST-API/
β”œβ”€β”€ node_modules/
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ controller/ # All controller logic (e.g., auth.controller.ts)
β”‚ β”œβ”€β”€ model/ # Mongoose models (e.g., user.model.ts)
β”‚ β”œβ”€β”€ routes/ # All API route files (e.g., auth.routes.ts)
β”‚ β”œβ”€β”€ app.ts # Main application entry point
β”‚ └── dbconnect.ts # MongoDB connection setup
β”œβ”€β”€ .env # Environment variables
β”œβ”€β”€ .gitignore
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json

Create a .gitignore file and add:

node_modules/
dist/
.env

βœ… What’s Next?​

In the next lesson, you’ll:

  • Connect your project to MongoDB
  • Write the database setup logic in dbconnect.ts