HR backend
HR backend
build RESTful APIs to handle various operations such as fetching jobs, candidates,
interviews, and company statuses, as well as handling job postings. Here's how you
can structure the backend using Node.js, Express, and MongoDB:
1. Project Setup
npm init -y
npm install express mongoose body-parser cors
// MongoDB connection
mongoose.connect('mongodb://localhost/hr_dashboard', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// Import routes
const jobsRouter = require('./routes/jobs');
const candidatesRouter = require('./routes/candidates');
const interviewsRouter = require('./routes/interviews');
const companyStatusRouter = require('./routes/companyStatus');
// Use routes
app.use('/api/jobs', jobsRouter);
app.use('/api/candidates', candidatesRouter);
app.use('/api/interviews', interviewsRouter);
app.use('/api/company-status', companyStatusRouter);
module.exports = router;
Candidates Routes (routes/candidates.js)
javascript
module.exports = router;
module.exports = router;
module.exports = router;
javascript
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Database Connection
mongoose.connect('mongodb://localhost:27017/hrportal', { useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('Failed to connect to MongoDB', err));
// Job Routes
try {
const newJob = await job.save();
res.status(201).json(newJob);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// Profile Routes
// Get all profiles
app.get('/api/profiles', async (req, res) => {
try {
const profiles = await Profile.find();
res.json(profiles);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
try {
let profile = await Profile.findOne({ email });
if (profile) {
// Update existing profile
profile.name = req.body.name;
profile.contactNumber = req.body.contactNumber;
profile.position = req.body.position;
profile.bio = req.body.bio;
} else {
// Create new profile
profile = new Profile({
name: req.body.name,
email: req.body.email,
contactNumber: req.body.contactNumber,
position: req.body.position,
bio: req.body.bio,
});
}
// Application Routes
try {
const newApplication = await application.save();
res.status(201).json(newApplication);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// Listen on a port
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Explanation:
Job Schema and Routes:
The job schema includes fields like title, description, location, company, and
salary.
Routes allow fetching all jobs and adding a new job.
Profile Schema and Routes:
The profile schema includes fields like name, email, contactNumber, position, and
bio.
Routes allow fetching all profiles and adding/updating a profile.
Application Schema and Routes:
The application schema links to a job using jobId and includes fields like
applicantName, applicantEmail, and resume.
Routes allow fetching all applications (with job details populated) and submitting
a job application.
Running the Server:
Ensure MongoDB is running on your system or provide a MongoDB connection string.
Install necessary packages using:
node HRserver.js
This server will handle routes for jobs, profiles, and applications all in one
place.