Node.js Express Dynamic Content Renderer
Overview
This project is a Node.js Express application that dynamically fetches and renders content from an external .NET API. It utilizes EJS templating for dynamic pages, compression for performance, and environment variables for configuration.
Features
Dynamic Page Rendering: Fetches article data from the .NET API and renders it using EJS.
Static File Serving: Uses express.static for serving CSS, JS, and images.
Image Proxying: Fetches images from an API and serves them correctly.
Compression: Improves performance using gzip compression.
Environment Variables: Uses .env for configurable settings.
Tech Stack
Node.js
Express.js
EJS (Embedded JavaScript for templating)
Fetch API for API calls
Compression for performance optimization
Dotenv for environment variables
Installation
- Clone the Repository
git clone https://github.com/yourusername/yourproject.git cd yourproject
- Install Dependencies
npm install
- Create a .env File
Create a .env file in the root directory and configure the necessary environment variables:
PORT=5000 NODE_ENV=development API_BASE_URL=https://apicosmosdocs.azurewebsites.net/api
- Run the Server
Development Mode (with auto-restart using Nodemon)
npm run dev
Production Mode
node server.js
Folder Structure
/your-project ├── server.js # Main Express server ├── routes/ │ ├── articles.js # API routing for dynamic content ├── views/ │ ├── index.ejs # Dynamic EJS template ├── public/ # Static assets (CSS, JS, images) │ ├── css/ │ │ ├── styles.css │ ├── js/ │ │ ├── script.js ├── .env # Environment variables ├── package.json # Project dependencies ├── README.md # Project documentation
API Endpoints
- Fetch an Article (Dynamic Rendering)
GET /some-article-path
Fetches article data from .NET API and renders it dynamically.
- Proxy Image Requests
GET /pub/*
Fetches images from the API and serves them correctly.
Troubleshooting
Styles Not Loading? Ensure Bootstrap is included in index.ejs:
Check Environment Variables:
echo $PORT # On Mac/Linux echo %PORT% # On Windows
License
MIT License
Author
Developed by CosmosCMS. Feel free to contribute!