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.
Top comments (5)
Very interesting app, thanks for the write up!
Thank you for your comment @dsaga
is there no need to setup the multer will it work without it
we do need to setup multer as it is attached to our route
app.post('/upload', upload.single('resume'), async (req, res) => {
@khaybee24 let me know if this is helpful
Yes, very much helpful thank you.