This file contains all the details you need to start your backend project. Follow these steps to get started:
Run the following command:
npm init
This will initialize a package for your folder. It will ask you some basic questions, such as:
- Package name
- Git repository
- Author
Note: If you’re a beginner, you can leave the test
folder empty or skip configuring it for now.
To start your server, use the following command:
npm start
Command:
npm i express
Why Use Express.js?
- Handle Requests: Easily set up ways to respond to user actions or API calls.
- Add Features: Quickly implement features like authentication or data handling.
- Save Time: Simplifies tasks, speeding up development.
Command:
npm i nodemon
Why Use Nodemon?
- Automatically restarts your server when changes are made.
- Simplifies your workflow by reflecting changes without manual restarts.
To configure Nodemon:
- Open the
package.json
file. - Locate the
scripts
section. - Replace
"node app.js"
with"nodemon app.js"
.
"scripts": {
"start": "nodemon app.js"
}
Command:
npm i jsonwebtoken
Why Use JWT?
- Secure Login: Allows secure communication between the server and client.
- Simple to Use: Easy to create, send, and verify tokens for authentication and authorization.
You can organize your project by creating a src
folder with the following subfolders:
-
models
Define the structure of your data and database models. Example: A "User" model might include fields likename
,email
, andpassword
. -
middleware
Add checkpoint functions that run before requests reach the controllers. Example: Check if a user is logged in or validate input data. -
db
Manage database connections and related functions. Example: A file here could connect to MongoDB or MySQL. -
controllers
Handle incoming requests and responses. Example: AUserController
might handle user-related actions like login or registration. -
routes
Define your API endpoints and map them to their respective controllers. -
utils
Store utility functions and reusable logic.
You can automate the creation of these folders using the following code snippet:
const fs = require("fs");
const folders = ['controllers', 'middleware', 'routes', 'utils', 'db', 'models'];
folders.forEach(folder => {
fs.mkdir(`./src/${folder}`, { recursive: true }, (err) => {
if (err) console.error(err);
else console.log(`${folder} folder created successfully`);
});
});
Command:
npm i mongoose
Why Use Mongoose?
- Simplifies working with MongoDB by providing schema-based models.
- Offers powerful features for database operations.
Command:
npm i bcrypt
Why Use Bcrypt?
- Hashes passwords securely before storing them in the database.
Create a .gitignore
file to exclude files or folders you don’t want to track with Git. Example:
.env
(to store sensitive environment variables)node_modules
Command:
npm i dotenv
Why Use Dotenv?
- Allows you to configure environment variables from a
.env
file. - Helps keep sensitive data like API keys secure.
Note: Never push your .env
file to a public repository.
Here’s an example of a basic index.js
file to set up your project:
import dotenv from "dotenv";
import express from "express";
dotenv.config({ path: './.env' });
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server is running on port ${port}`));
Once your setup is complete:
- Create your database models.
- Connect your database using Mongoose or any preferred database library.
- Implement features like login/logout, user authentication, and data handling.
- Take time to explore advanced features and best practices as your project grows.
- Customize your folder structure and code to meet specific project requirements.
- Keep learning and improving!
By following these steps, you’ll have a well-structured Node.js project ready to scale and maintain.