Deployment Guide
Relevant source files
The following files were used as context for generating this wiki page:This guide provides instructions for deploying the LostData application. The application is a Node.js web server built with the Express framework, which serves a static single-page frontend and provides a REST API for managing a "Model Database". The backend connects to a MongoDB database for data persistence.
The deployment process involves setting up the Node.js environment, installing dependencies, configuring the database connection, and running the server. An optional database seeding step is also available to populate the application with initial data.
System Architecture
The application consists of three main components: a client-side interface, a backend API server, and a MongoDB database. The Node.js server uses Express to handle API requests and to serve the static frontend files located in the public directory. All application data is stored in a MongoDB database, with Mongoose used as the Object Data Modeling (ODM) library.
Sources: server.js, public/index.html, APP/config/db.config.js
The following diagram illustrates the high-level architecture of the system.
Sources: server.js:21, 25, 147-149
Prerequisites
Before deploying the application, ensure the following software is installed on the target machine:
- Node.js: The runtime environment for the application.
- npm: The package manager for Node.js, used to install dependencies.
- MongoDB: The database used for data storage. A connection URI is required.
Sources: package.json, server.js:25
Configuration
The application is configured using environment variables. It is recommended to use a .env file in the project root to manage these variables. The dotenv package is used to load them automatically.
Sources: server.js:1
Environment Variables
| Variable | Description | Default Value | Source File |
|---|---|---|---|
MONGODB_URI | The connection string for the MongoDB database. | mongodb://127.0.0.1:27017/model_db | APP/config/db.config.js:2 |
PORT | The port on which the Express server will listen. | 3000 | server.js:151 |
Session Management
Session management uses express-session with a hardcoded secret key. For a production environment, it is critical to change this key to a secure, randomly generated string and manage it as an environment variable.
app.use(session({
secret: 'secret-key', // <-- IMPORTANT: Change for production
resave: false,
saveUninitialized: true
}));
Sources: server.js:17-21
Installation and Setup
Follow these steps to install and set up the application.
1. Install Dependencies
Navigate to the project's root directory and run npm install to install all the required packages listed in package.json.
npm install
The key dependencies are:
| Package | Version | Description |
|---|---|---|
express | ^5.1.0 | Web framework for Node.js. |
mongoose | ^8.19.0 | MongoDB object modeling tool. |
cors | ^2.8.5 | Middleware for enabling Cross-Origin Resource Sharing. |
dotenv | ^17.2.3 | Loads environment variables from a .env file. |
express-session | ^1.18.2 | Session middleware. |
multer | ^2.0.2 | Middleware for handling multipart/form-data (file uploads). |
csv-parser | ^3.2.0 | Streaming CSV parser. |
json2csv | ^6.0.0-alpha.2 | Converts JSON to CSV. |
Sources: package.json
2. Database Seeding (Optional)
The repository includes a seed.js script to populate the database with initial model data. This script connects to the MongoDB instance defined by the MONGODB_URI and inserts a predefined set of model records.
To run the seeder, execute the following command from the project root:
node seed.js
Upon successful completion, it will print "Models seeded!" to the console.
Sources: seed.js:1, 126-133
Running the Application
Once the dependencies are installed and the environment is configured, start the server with the following command:
node server.js
The server will start, connect to the database, and listen on the configured port. You should see the following output in your console:
DB connected
Server on 3000
Sources: server.js:25, 152
Application Components
Backend Server
The backend is an Express application defined in server.js. It handles API logic, user authentication (session-based), and serving the frontend.
API Endpoints
The server exposes several REST API endpoints under the /api prefix. Admin-only endpoints are protected by the isAdmin middleware, which checks if the user's role is 'admin'.
The following sequence diagram shows the flow for a user fetching the list of all models.
Sources: public/index.html:121-125, server.js:56-63
Database Schema
The application uses a single Mongoose schema for "Models". The schema defines the structure of the model documents stored in MongoDB. It includes a history array that automatically logs updates to a document.
Sources: APP/models/model.model.js, APP/models/index.js
The diagram below shows the entity relationship for the Model collection.
Sources: APP/models/model.model.js:3-13
The history field is automatically populated on document updates by a pre('save') hook, capturing the timestamp, action, and modified fields.
schema.pre('save', function(next) {
if (this.isModified() && !this.isNew) {
this.history.push({
timestamp: new Date(),
action: 'update',
changes: this.modifiedPaths()
});
}
next();
});
Sources: APP/models/model.model.js:18-27
Frontend
The user interface is a single-page application contained in public/index.html. It interacts with the backend API to perform all operations, including login, fetching and displaying models, searching, and triggering import/export actions. The UI is built with HTML and vanilla JavaScript, styled with Tailwind CSS.
Sources: public/index.html