Creating a simple application in JavaScript to handle personal records involves several steps. We’ll use a combination of front-end code (HTML and JavaScript) for the user interface and a simple back-end using Node.js with the express library for handling routes. We'll use a JSON file as our database for simplicity since we're not using a real database system. You can extend this to use a real database like MongoDB in a production environment.
Setting Up the Project
-
Initialize the Project: Create a project directory and initialize a Node.js project.
mkdir PersonalRecordsApp
cd PersonalRecordsApp
npm init -y
-
Install Required Packages:
npm install express body-parser
-
Create Basic File Structure:
index.js: The main server file.
records.json: To store the records.
public/index.html: The front-end HTML file.
Setting Up the Server (index.js)
Here's a simple server using Express.js:
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;
// Middleware setup
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// Function to read/write to the records.json file
const readRecords = () => JSON.parse(fs.readFileSync('records.json', 'utf8'));
const writeRecords = (data) => fs.writeFileSync('records.json', JSON.stringify(data, null, 2));
// API Endpoints
app.get('/api/records', (req, res) => {
const records = readRecords();
res.json(records);
});
app.post('/api/records', (req, res) => {
const records = readRecords();
const newRecord = req.body;
records.push(newRecord);
writeRecords(records);
res.json(newRecord);
});
app.delete('/api/records/:nationalNumber', (req, res) => {
const nationalNumber = req.params.nationalNumber;
let records = readRecords();
records = records.filter(record => record.nationalNumber !== nationalNumber);
writeRecords(records);
res.json({ message: 'Record deleted' });
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Front-End HTML (public/index.html)
Create a simple front-end using HTML and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Personal Records</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const recordForm = document.getElementById('recordForm');
const recordsTable = document.getElementById('recordsTableBody');
// Load records
const loadRecords = async () => {
const response = await fetch('/api/records');
const records = await response.json();
recordsTable.innerHTML = '';
records.forEach(record => {
const row = recordsTable.insertRow();
Object.keys(record).forEach(key => {
const cell = row.insertCell();
cell.textContent = record[key];
});
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.onclick = () => deleteRecord(record.nationalNumber);
deleteCell.appendChild(deleteButton);
});
};
// Add new record
recordForm.onsubmit = async (e) => {
e.preventDefault();
const formData = new FormData(recordForm);
const record = Object.fromEntries(formData.entries());
await fetch('/api/records', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(record)
});
recordForm.reset();
loadRecords();
};
// Delete record
const deleteRecord = async (nationalNumber) => {
await fetch(`/api/records/${nationalNumber}`, { method: 'DELETE' });
loadRecords();
};
loadRecords();
});
</script>
</head>
<body>
<h1>Personal Records</h1>
<form id="recordForm">
<input type="text" name="name" placeholder="Name" required>
<input type="text" name="fatherName" placeholder="Father's Name" required>
<input type="text" name="nationalNumber" placeholder="National Number" required>
<input type="date" name="dob" required>
<input type="text" name="education" placeholder="Educational Qualification" required>
<input type="text" name="telephone" placeholder="Telephone Number" required>
<input type="text" name="position" placeholder="Position" required>
<input type="text" name="address" placeholder="Address" required>
<button type="submit">Add Record</button>
</form>
<table>
<thead>
<tr>
<th>Name</th>
<th>Father's Name</th>
<th>National Number</th>
<th>Date of Birth</th>
<th>Educational Qualification</th>
<th>Telephone Number</th>
<th>Position</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="recordsTableBody"></tbody>
</table>
</body>
</html>
Explanation
-
Server (index.js):
- Uses Express to handle HTTP requests and
body-parser for parsing JSON bodies.
- Provides endpoints to get all records, add a new record, and delete a record by
nationalNumber.
- Interacts with a JSON file (
records.json) to store and retrieve records.
-
Front-End (index.html):
- Form for inputting personal data.
- Table to display stored records.
- JavaScript to load, add, and delete records by making AJAX calls to the server.
Note
- This is a basic demonstration that uses a JSON file to simulate a database. For a production application, consider using a real database (e.g., MongoDB, SQLite, etc.).
- Error handling and validation should be improved for a real-world application.
- For deployment, you'd also set this up to use a cloud provider like Heroku or Vercel, and possibly a real database.
Ensure that records.json is initialized as an empty array:
[]
Running the Project
- Run the server:
node index.js
- Open your browser and visit
http://localhost:3000 to interact with the application.