Currently Empty: $0.00
Blog
How to Develop and Deploy a Web Application with Node.js

Developing and deploying a web application with Node.js is a streamlined process often favored for its speed and use of a single language (JavaScript) across the entire stack. This guide will walk you through the key phases: Development (using Express.js), Testing, and Deployment (using Docker and a cloud service).
1. Development: Setting Up the Node.js Application
We’ll use Express.js, the minimal and flexible Node.js web application framework, as it’s the industry standard for building APIs and web servers.
A. Initialize the Project
- Create a Project Folder:Bash
mkdir my-node-app cd my-node-app - Initialize Node.js: This creates your
package.jsonfile.Bashnpm init -y - Install Dependencies:Bash
npm install express - Create the Server File: Create an
index.jsfile to start your server.
B. Write the Server Code (index.js)
Your server file handles routes and starts the listener.
JavaScript
const express = require('express');
const app = express();
const port = 3000;
// Middleware to parse JSON request bodies
app.use(express.json());
// Basic GET route
app.get('/', (req, res) => {
res.send('Hello from the Node.js Server!');
});
// A simple API endpoint (POST route)
app.post('/api/data', (req, res) => {
const data = req.body;
console.log('Received data:', data);
// In a real app, you'd save this to a database
res.status(201).json({ message: 'Data received successfully', received: data });
});
// Start the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
C. Configure Scripts
Edit your package.json file to include a start script:
JSON
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js" // Optional: Use nodemon for automatic restarts during development
},
D. Testing the Server
Run your application locally and test the endpoints using a tool like Postman or by visiting the root URL in your browser.
Bash
npm start
2. Preparing for Deployment with Docker
Using Docker is the modern best practice for deployment because it packages your application and its dependencies into a consistent, portable environment (a container).
A. Create a Dockerfile
Create a file named Dockerfile (no extension) in your project root.
Dockerfile
# 1. Base Image: Use a stable Node.js runtime
FROM node:18-alpine
# 2. Working Directory: Set the directory inside the container
WORKDIR /usr/src/app
# 3. Copy Dependency Files: Copy package.json and package-lock.json first
COPY package*.json ./
# 4. Install Dependencies: Run npm install
RUN npm install
# 5. Copy Application Source Code
COPY . .
# 6. Expose Port: The port the app listens on
EXPOSE 3000
# 7. Startup Command: Define the command to run the app
CMD [ "npm", "start" ]
B. Create a .dockerignore File
Create a file named .dockerignore to exclude large, unnecessary files (like source control data and local dependency folders) from being copied into the image, which speeds up the build process.
node_modules
npm-debug.log
.git
.gitignore
C. Build the Docker Image
Run this command in your project directory:
Bash
docker build -t my-node-app-image .
D. Run the Docker Container (Optional Local Test)
Test the container locally to ensure it works as expected before pushing to the cloud.
Bash
docker run -p 80:3000 my-node-app-image
Your app will now be accessible at http://localhost:80.
3. Deployment to a Cloud Service
The final step is deploying your application to a hosting environment. We’ll outline the common approaches using either Platform as a Service (PaaS) or Container-as-a-Service.
A. Option 1: Using Platform as a Service (PaaS) – e.g., Heroku, Render
PaaS solutions are the easiest for beginners as they handle much of the infrastructure setup (servers, scaling) automatically.
- Prepare the
Procfile(Heroku/Render): Create aProcfile(no extension) that tells the platform how to start your app.web: npm start - Connect to Git: Push your code to a Git repository (GitHub/GitLab).
- Deploy via Dashboard: Connect your PaaS account to your Git repository and trigger an automatic deployment. The platform detects the Node.js files and runs the
npm startscript specified in yourpackage.json.
B. Option 2: Using Container Platforms – e.g., AWS ECS, Google Cloud Run, Azure Container Instances
This method uses the Docker image you already created, offering more control.
- Push Image to Registry: Upload your built Docker image to a public or private container registry (e.g., Docker Hub, AWS ECR, Google Artifact Registry).Bash
docker tag my-node-app-image [your-registry-username]/my-node-app-image:latest docker push [your-registry-username]/my-node-app-image:latest - Configure Service: In your chosen cloud platform’s dashboard (e.g., Google Cloud Run or AWS ECS), create a new service:
- Specify the Image URL you just pushed.
- Set the Port to
3000(the port the container exposes). - Set desired scaling rules (e.g., minimum and maximum instances).
- Launch: The platform pulls the image, creates containers, and manages traffic routing, making your application live and scalable.
C. Environment Variables (Critical for Production)
Never hardcode database credentials or API keys. Use environment variables defined in your cloud hosting environment.
- In your Node.js code, access them via
process.env.VARIABLE_NAME. - During deployment, set the actual values (e.g.,
PORT=3000,DATABASE_URL=...) in the PaaS dashboard or container configuration settings

