Building A Resume Upload And Parsing App With NodeJs And MongoDb
FredAbod
Posted on February 2, 2024
Lets Take A DIVE IN
In this tutorial, i'll guide you through the process of creating a Node.js application that allows users to upload their resumes, extract information from them, and store the data in MongoDB. We'll use Express for the web server, Multer for file uploads, pdf-parse for PDF text extraction, and Mongoose for MongoDB integration.
Prerequisites
Make sure you have Node.js and npm installed on your machine. You can download them from Node.js official website.
npm init -y
npm install express multer pdf-parse mongoose
Project Setup
- Create an
app.js
file for your Node.js application. - Set up Express, Multer, and MongoDB connection in
app.js
.
// app.js
const express = require('express');
const multer = require('multer');
const pdfParse = require('pdf-parse');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
mongoose.connect('mongodb://localhost:27017/resumeApp', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// ... (resume model and multer setup)
// ... (upload endpoint and data extraction)
Resume Model
Add a Resume Model
// Create a Resume model
const Resume = mongoose.model("Resume", {
name: String,
email: String,
phone: String,
content: String,
});
File Upload and Parsing
Set up Multer to handle file uploads and pdf-parse to extract text from resumes.
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.post('/upload', upload.single('resume'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const buffer = req.file.buffer;
const data = await pdfParse(buffer);
const resumeData = {
name: extractName(data.text),
email: extractEmail(data.text),
phone: extractPhone(data.text),
content: data.text,
};
const savedResume = await new Resume(resumeData).save();
res.json({ success: true, resume: savedResume });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
});
// ... (extractName, extractEmail, extractPhone functions)
Resume Data Extraction Functions
Define functions to extract name, email, and phone number from the resume text.
function extractName(text) {
const nameRegex = /([a-zA-Z]+[a-zA-Z\s]+)/;
const match = text.match(nameRegex);
return match ? match[0] : '';
}
function extractEmail(text) {
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
const match = text.match(emailRegex);
return match ? match[0] : '';
}
function extractPhone(text) {
const phoneRegex = /(\+\d{1,2}\s?)?(\d{10,})/;
const match = text.match(phoneRegex);
return match ? match[0] : '';
}
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Start up your App and Test your app with Postman
Here's how mine look like
Congratulations! You've successfully created a Node.js application that allows users to upload resumes, extracts relevant information, and stores it in MongoDB.
Posted on February 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
June 11, 2024