This is a simple e-commerce application built with Node.js and Express.js that demonstrates how to create an HTTPS server using Node.js with Let's Encrypt SSL certificates.
The application automatically switches between HTTP (development) and HTTPS (production) modes based on environment configuration. In production mode, it uses Let's Encrypt certificates to serve secure HTTPS connections and includes an HTTP redirect server that automatically redirects all HTTP traffic to HTTPS.
demo-node-app/
├── app.js # Main Express application
├── server.js # Server entry point (handles HTTP/HTTPS based on environment)
├── controllers/ # Controller files
│ ├── productController.js
│ └── userController.js
├── routes/ # Route files
│ ├── productRoutes.js
│ └── userRoutes.js
├── data/ # Mock data files
│ ├── products.json
│ └── users.json
└── public/ # Static files
├── index.html
├── cart.html
├── script.js
└── cart.js
Install dependencies:
npm installThe server.js file dynamically configures the server based on the NODE_ENV environment variable:
-
Development Mode (default, when
NODE_ENVis not set or not "production"):- Runs HTTP server only
- Default port:
3000 - Accessible at
http://localhost:3000 - No
.envfile required
-
Production Mode (
NODE_ENV=production):- Runs HTTPS server on port
443 - Runs HTTP redirect server on port
80(redirects to HTTPS) - Uses Let's Encrypt SSL certificates
- Domain configured via
DOMAINenv variable - Requires
.envfile with production settings
- Runs HTTPS server on port
Start the server:
npm startFor development with auto-reload:
npm run devThe application will be available at http://localhost:3000
- Create a
.envfile in the root directory with production settings:
NODE_ENV=production
DOMAIN=yourdomain.com- Start the server:
npm startFor production deployment with HTTPS, you need to generate SSL certificates using Let's Encrypt.
- Update your system packages:
sudo apt update
sudo apt install -y certbot- Important: Make sure your server is NOT running before generating certificates.
Run the following command to generate certificates for your domain (replace yourdomain.com with your actual domain):
sudo certbot certonly --standalone -d yourdomain.comIf successful, certificates will be created here:
/etc/letsencrypt/live/yourdomain.com/
The certificate files include:
privkey.pem- Private keyfullchain.pem- Certificate chain
To verify your certificates are valid and check their expiration:
sudo certbot certificatesLet's Encrypt certificates expire after 90 days. To renew:
sudo certbot renewGET /api/products- Get all productsGET /api/products/:id- Get product by IDGET /api/products/category/:category- Get products by category
GET /api/users- Get all usersGET /api/users/:id- Get user by ID
- Open your browser and navigate to
http://localhost:3000 - Browse products using the category filters
- Click "Load Users" to view all registered users
- Use the "Add to Cart" button on products (currently shows an alert)
Create a .env file in the root directory:
NODE_ENV=development # Set to "production" for HTTPS mode
PORT=3000 # Port for development server (default: 3000)
DOMAIN=yourdomain.com # Domain name for production (used for SSL certificates)- All API routes are prefixed with
/api - Products are stored in
data/products.json - Users are stored in
data/users.json - The UI uses Tailwind CSS via CDN with a dark theme and blue color scheme
- The server automatically switches between HTTP (development) and HTTPS (production) based on
NODE_ENV - For production, ensure SSL certificates are generated and the server has read access to
/etc/letsencrypt/live/{domain}/