DEV Community

SWAPNIL AHMMED SHISHIR
SWAPNIL AHMMED SHISHIR

Posted on

πŸš€ Step-by-Step Guide to Building a Node.js Server with Express

  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

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


Enter fullscreen mode Exit fullscreen mode

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

npm install --save-dev nodemon

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

Enter fullscreen mode Exit fullscreen mode

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('<h1>Welcome to the Running Server</h1>');
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});

Enter fullscreen mode Exit fullscreen mode
  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

Enter fullscreen mode Exit fullscreen mode

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/

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

9.When ready, update all packages:

npm install -g npm-check-updates
ncu -u
npm install
Enter fullscreen mode Exit fullscreen mode

Top comments (0)