Initial App Setup
The Pit Crew: Installing Dependencies
Section titled “The Pit Crew: Installing Dependencies”Before we hit the track, we need to equip our package.json with the necessary tools. This is our pit stop.
We are organizing our logic into folders (data, routers, views) to keep the project scannable as it speeds up.
1. Initialize & Install
Section titled “1. Initialize & Install”In the terminal, navigate to the project folder:
npm init -ynpm install express mongoose ejs dotenvWe’re using node --watch (native to Node v18+) for development speed. It
restarts the server automatically when we save a file, just like nodemon
used to do.
2. Configure Environment Variables
Section titled “2. Configure Environment Variables”Create a .env file in the root directory. This is where we store our sensitive connection string.
MONGO_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/<database-name>?retryWrites=true&w=majorityPORT=3000Make sure to replace <username>, <password>, and <database-name> with your actual MongoDB credentials.
Also, make sure your .env file is ignored in your .gitignore file (lest you commit your credentials to GitHub for all to see!).
3. The Project Blueprint
Section titled “3. The Project Blueprint”For the rest of this chapter, we’re building a Project Pulse application. It’s a portfolio manager where we can track our coding projects, their status (active/archived), and descriptions.
Directory Structure:
/my-project-pulse ├── /data # Mongoose Models (Schemas) ├── /public # Static assets (CSS, images) ├── /routers # Express Routes ├── /views # EJS Templates ├── .env # Environment Variables ├── server.js # Entry Point └── package.json # Dependencies4. Boilerplate: server.js
Section titled “4. Boilerplate: server.js”Before we connect to the database in the next lesson, let’s get our standard Express server running.
This is our “clean slate.” Copy this into server.js. It includes Express,
EJS, and the basic middleware, but no Mongoose yet. That comes next.
require("dotenv").config();const express = require("express");const app = express();const port = process.env.PORT || 3000;
// Middlewareapp.use(express.urlencoded({ extended: true })); // Parse form dataapp.use(express.static("public")); // Serve static files
// View Engineapp.set("view engine", "ejs");
// Temporary Route (Just to check if it works)app.get("/", (req, res) => { res.send("<h1>Server is Running! Next Stop: MongoDB.</h1>");});
// Start Serverapp.listen(port, () => { console.log(`Server running on http://localhost:${port}`);});⏭ Next: Open the Gates to MongoDB
Section titled “⏭ Next: Open the Gates to MongoDB”Boilerplate is ready. The chassis is built. Now let’s get some data pipes going.