Write a program with JavaScript
Take the name, father's name, national number, date of birth, educational qualification, telephone number, position and address and save it in a text file.
The database will be displayed
It is possible to delete and search based on all records in the database
Assistant
Creating a simple CRUD (Create, Read, Update, Delete) application in JavaScript that allows you to take user input and save it, display the records, and perform search and deletion operations can involve both client-side and server-side logic. Below is a simple example using Node.js for server-side handling.
For simplicity, we will avoid using a database and instead use a JSON file to store the data. The records will be stored in a JSON array. Here's how you can achieve this:
Setup:
Install Node.js if you haven't already.
Create a new directory for your project and navigate into it.
Run npm init -y to set up a new Node.js project.
Install the necessary npm packages:
bash
npm install express body-parser fs
Server-Side Code (Node.js with Express):
Create a file called server.js and add the following code:
javascript
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const PORT = 3000;
// Middleware
app.use(bodyParser.json());
const FILE_PATH = 'database.json';
// Helper function to read data from file
function readDataFile() {
if (fs.existsSync(FILE_PATH)) {
const data = fs.readFileSync(FILE_PATH);
return JSON.parse(data);
} else {
return [];
}
}
// Helper function to write data to file
function writeDataFile(data) {
fs.writeFileSync(FILE_PATH, JSON.stringify(data, null, 2));
}
// Route to add a new record
app.post('/add', (req, res) => {
const newRecord = req.body;
const data = readDataFile();
data.push(newRecord);
writeDataFile(data);
res.status(201).send('Record added successfully.');
});
// Route to get all records
app.get('/records', (req, res) => {
const data = readDataFile();
res.json(data);
});
// Route to search records
app.get('/search', (req, res) => {
const query = req.query;
const data = readDataFile();
const results = data.filter(record =>
Object.entries(query).every(([key, value]) => record[key] === value)
);
res.json(results);
});
// Route to delete a record
app.delete('/delete/:nationalNumber', (req, res) => {
const nationalNumber = req.params.nationalNumber;
let data = readDataFile();
const originalLength = data.length;
data = data.filter(record => record.nationalNumber !== nationalNumber);
if (data.length !== originalLength) {
writeDataFile(data);
res.send('Record deleted successfully.');
} else {
res.status(404).send('Record not found.');
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Explanation:
We're using Express to handle HTTP requests.
Body-parser is used to parse incoming request bodies in a middleware.
We use fs (File System) to read from and write to a JSON file that acts as our database.
There are routes to add, search, retrieve, and delete records.
Client-Side Code (HTML Form):
For the client-side, you can create an HTML file with a form to collect user inputs and interact with the server using fetch or AJAX:
Open the HTML file in a browser to start adding, viewing, and managing records.
This is a highly simplified version and doesn't cover many real-world concerns, such as input validation and authentication, but it provides a basic CRUD operation setup using JavaScript.