Write a program with JavaScript
to take the father's name, national number, date of birth, educational qualification, phone number, position and address and store it in a database.
The database will be displayed
It is possible to delete and search in the database
Assistant
Creating a simple JavaScript application to manage a database of this nature typically involves using both the front-end and back-end. Below is a simple guide to build a basic application using JavaScript, Node.js with Express for the backend, and MongoDB as the database.
Install MongoDB locally or use a cloud-based MongoDB service like MongoDB Atlas.
Project Setup
Initialize Node.js Project
shell
mkdir personalRecords
cd personalRecords
npm init -y
Install Dependencies
shell
npm install express mongoose body-parser cors
Create Server File
Create a file named server.js and write the following code:
javascript
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/personalRecords', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Connected to the database');
});
// Create Schema
const recordSchema = new mongoose.Schema({
fatherName: String,
nationalNumber: String,
dateOfBirth: Date,
educationalQualification: String,
phoneNumber: String,
position: String,
address: String,
});
const Record = mongoose.model('Record', recordSchema);
// CRUD Operations
// Create a new record
app.post('/add', (req, res) => {
const newRecord = new Record(req.body);
newRecord.save((err) => {
if (err) return res.status(500).send(err);
return res.status(200).send('Record added successfully!');
});
});
// Retrieve all records
app.get('/records', (req, res) => {
Record.find({}, (err, records) => {
if (err) return res.status(500).send(err);
return res.status(200).json(records);
});
});
// Delete a record by ID
app.delete('/delete/:id', (req, res) => {
Record.findByIdAndRemove(req.params.id, (err, record) => {
if (err) return res.status(500).send(err);
return res.status(200).send('Record deleted successfully!');
});
});
// Search records by father's name
app.get('/search', (req, res) => {
const query = req.query.fatherName;
Record.find({ fatherName: new RegExp(query, 'i') }, (err, records) => {
if (err) return res.status(500).send(err);
return res.status(200).json(records);
});
});
// Start Server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Client-side Example (HTML)
Create an index.html file to create a simple interface for sending requests. For simplicity, we'll use a mix of JavaScript and HTML:
html
Run the Application
Start the server with:
shell
node server.js
Open index.html in a web browser. This will give you a simple interface to add, view, search, and delete records.
Notes:
This example uses MongoDB running locally (mongodb://localhost:27017/personalRecords). Change this to your MongoDB connection string if using a remote instance.
The provided client-side example uses the Fetch API to communicate with the backend. Make sure you run this on a browser and have Node.js and MongoDB running.
The application does not include advanced error handling or validation; additional logic would be required for a production-ready application.
For any real-world application, ensure proper security practices such as input validation, authentication, and authorization.