Local Development Setup
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the necessary steps and components to set up and run the LostData project on a local development machine. The project is a full-stack application consisting of a Node.js/Express backend, a MongoDB database, and a vanilla JavaScript frontend. It serves as a database for "models," allowing for CRUD operations, data import/export, and role-based access control.
The core of the application is an Express server that provides a REST API for managing model data. The frontend is a single-page application that consumes this API to display and interact with the data. Data persistence is handled by a MongoDB database, with a Mongoose schema defining the data structure.
Architecture Overview
The local development environment consists of three main components: the Frontend Client, the Backend Server, and the Database.
This diagram shows the basic interaction flow: the frontend makes API calls to the Express server, which in turn communicates with the MongoDB database to perform data operations.
Sources: server.js, public/index.html, APP/models/index.js
Prerequisites and Dependencies
To run the project, a Node.js environment and a running MongoDB instance are required. The necessary Node.js packages are listed in package.json.
| Dependency | Purpose |
|---|---|
express | Web framework for Node.js, used to build the server and API. |
mongoose | MongoDB object modeling tool for Node.js. |
cors | Middleware to enable Cross-Origin Resource Sharing. |
dotenv | Loads environment variables from a .env file. |
express-session | Middleware for managing user sessions (used for login). |
multer | Middleware for handling multipart/form-data, used for file uploads. |
csv-parser | Parses CSV files, used for the import feature. |
json2csv | Converts JSON to CSV, used for the export feature. |
Sources: package.json:2-10
Install these dependencies using a package manager:
npm install
Configuration
The application requires environment variables for configuration, which can be placed in a .env file at the root of the project.
| Variable | Description | Default Value |
|---|---|---|
MONGODB_URI | The connection string for the MongoDB database. | mongodb://127.0.0.1:27017/model_db |
PORT | The port on which the Express server will listen. | 3000 |
Sources: server.js:1, 26, 143, APP/config/db.config.js:2
Example .env file:
MONGODB_URI=mongodb://127.0.0.1:27017/model_db
PORT=3000
Database Setup
The application uses MongoDB for data storage. The connection is managed by Mongoose.
Sources: server.js:25-26, APP/models/index.js, APP/config/db.config.js
Model Schema
The data is structured according to the Model schema defined in Mongoose. This schema includes fields for model information, pricing, and an automatically updated history of changes.
The diagram above illustrates the fields and types for the Model collection in the database.
Sources: APP/models/model.model.js:2-15
The schema fields are detailed below:
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | required, unique | A unique identifier for the model. |
name | String | required | The name of the model. |
age | Number | - | The age of the model. |
location | String | - | The location of the model. |
profile | String | required | A detailed profile description. |
pricing | String | required | Pricing information. |
dosAndDonts | String | required | A list of allowed and disallowed activities. |
extraTabs | Array | default: [] | An array for additional information tabs. |
history | Array | default: [] | An audit log of changes made to the record. Automatically updated on save. |
createdAt | Date | (timestamps: true) | Timestamp of when the document was created. |
updatedAt | Date | (timestamps: true) | Timestamp of the last update. |
Sources: APP/models/model.model.js:4-16
A pre-save hook automatically populates the history array with changes whenever an existing document is modified.
Sources: APP/models/model.model.js:18-27
Data Seeding
The seed.js script can be used to populate the database with initial model data. It connects to the MongoDB instance specified in the configuration and inserts a predefined set of model records.
This flowchart shows the execution process of the seed.js script.
To run the seeder:
node seed.js
Sources: seed.js
Running the Application
Once dependencies are installed and the environment is configured, start the server from the project root:
node server.js
The server will perform the following actions on startup:
- Load environment variables from
.env. - Connect to the MongoDB database.
- Set up Express middleware for CORS, JSON parsing, URL encoding, and sessions.
- Define API routes for login, logout, and model CRUD operations.
- Serve static files from the
publicdirectory. - Listen for incoming requests on the configured
PORT.
Upon successful startup, you will see DB connected and Server on [PORT] logged to the console. You can then access the application in your browser at http://localhost:3000.
Sources: server.js:1-29, 140-144
API Endpoints
The server.js file defines a RESTful API for interacting with the model data. Access to modification endpoints (POST, PUT, DELETE) and data export is restricted to admin users.
Authentication
POST /api/login: Logs a user in. An email ending in@admin.comgrants 'admin' role, otherwise 'chatter' role. The role is stored in the session.POST /api/logout: Destroys the user session.
Sources: server.js:28-41
Model API
The following diagram shows the sequence for fetching all models.
Sources: server.js:50-57, public/index.html:92-96
| Method | Path | Admin | Description |
|---|---|---|---|
GET | /api/models | No | Retrieves a sorted list of all models. |
GET | /api/models/:id | No | Retrieves a single model by its _id. |
POST | /api/models | Yes | Creates a new model. |
PUT | /api/models/:id | Yes | Updates an existing model by its _id. |
DELETE | /api/models/:id | Yes | Deletes a model by its _id. |
GET | /api/export/json | Yes | Exports all models as a JSON file. |
GET | /api/export/csv | Yes | Exports all models as a CSV file. |
POST | /api/import/csv | Yes | Imports models from an uploaded CSV file. |
Sources: server.js:50-132