Initialize Your Project Create a new project directory and initialize it with npm: mkdir my-node-server cd my-node-server npm init -y Install Essential Dependencies Install the necessary packages for your server: npm install express cors dotenv cookie-parser body-parser jsonwebtoken bcrypt uuid For development convenience, install nodemon to automatically restart the server on code changes: npm install --save-dev nodemon Configure package.json Update your package.json to include the following: { "type": "module", "scripts": { "dev": "nodemon index.js" } } Setting "type": "module" enables ES6 module syntax. Create the Server Entry Point In the root directory, create an index.js file with the following content: import express from 'express'; import dotenv from 'dotenv'; import cors from 'cors'; import cookieParser from 'cookie-parser'; import bodyParser from 'body-parser'; dotenv.config(); const app = express(); // Middleware app.use(express.json()); app.use(cookieParser()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); // CORS configuration const allowedOrigins = [ 'http://localhost:5173', 'http://localhost:5174', 'http://localhost:3000' ]; app.use(cors({ origin: allowedOrigins, methods: ['GET', 'POST', 'PUT', 'DELETE'], credentials: true })); // Static files app.use(express.static('public')); // Routes app.get('/', (req, res) => { res.send('Welcome to the Running Server'); }); // Start the server const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`); }); Set Up Environment Variables Create a .env file in the root directory to store environment-specific variables:​ PORT=8080 JWT_SECRET=your_jwt_secret_key DB_HOST=localhost DB_USER=root DB_PASSWORD=your_db_password DB_NAME=your_db_name Ensure that .env is listed in your .gitignore to prevent sensitive information from being committed to version control. Create a .gitignore File In the root directory, create a .gitignore file with the following content: # Node modules node_modules/ # Logs logs *.log npm-debug.log* # Environment variables .env # OS-specific files .DS_Store # IDE-specific files .vscode/ .idea/ Start the Development Server Run the server in development mode using nodemon: npm run dev wow! you are creating your frist server . welcome to the server should now be running at http://localhost:8080. if need a any package update please follow this npm outdated 9.When ready, update all packages: npm install -g npm-check-updates ncu -u npm install

Apr 19, 2025 - 17:16
 0
  1. Initialize Your Project Create a new project directory and initialize it with npm:
mkdir my-node-server
cd my-node-server
npm init -y

  1. Install Essential Dependencies Install the necessary packages for your server:
npm install express cors dotenv cookie-parser body-parser jsonwebtoken bcrypt uuid


For development convenience, install nodemon to automatically restart the server on code changes:

npm install --save-dev nodemon

  1. Configure package.json Update your package.json to include the following:
{
  "type": "module",
  "scripts": {
    "dev": "nodemon index.js"
  }
}

Setting "type": "module" enables ES6 module syntax.

  1. Create the Server Entry Point In the root directory, create an index.js file with the following content:
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';

dotenv.config();

const app = express();

// Middleware
app.use(express.json());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// CORS configuration
const allowedOrigins = [
  'http://localhost:5173',
  'http://localhost:5174',
  'http://localhost:3000'
];

app.use(cors({
  origin: allowedOrigins,
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  credentials: true
}));

// Static files
app.use(express.static('public'));

// Routes
app.get('/', (req, res) => {
  res.send('

Welcome to the Running Server

'); }); // Start the server const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`); });
  1. Set Up Environment Variables Create a .env file in the root directory to store environment-specific variables:​
PORT=8080
JWT_SECRET=your_jwt_secret_key
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_db_password
DB_NAME=your_db_name

Ensure that .env is listed in your .gitignore to prevent sensitive information from being committed to version control.

  1. Create a .gitignore File In the root directory, create a .gitignore file with the following content:
# Node modules
node_modules/

# Logs
logs
*.log
npm-debug.log*

# Environment variables
.env

# OS-specific files
.DS_Store

# IDE-specific files
.vscode/
.idea/

  1. Start the Development Server Run the server in development mode using nodemon:
npm run dev

wow! you are creating your frist server . welcome to the server should now be running at http://localhost:8080.

  1. if need a any package update please follow this
npm outdated

9.When ready, update all packages:

npm install -g npm-check-updates
ncu -u
npm install