Building A Resume Upload And Parsing App With NodeJs And MongoDb

fredabod

FredAbod

Posted on February 2, 2024

Building A Resume Upload And Parsing App With NodeJs And MongoDb

Lets Take A DIVE IN

Diving Gif

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
Enter fullscreen mode Exit fullscreen mode

Project Setup

  1. Create an app.js file for your Node.js application.
  2. 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)
Enter fullscreen mode Exit fullscreen mode

Resume Model

Add a Resume Model

// Create a Resume model
const Resume = mongoose.model("Resume", {
  name: String,
  email: String,
  phone: String,
  content: String,
});
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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}`);
});
Enter fullscreen mode Exit fullscreen mode

Start up your App and Test your app with Postman

Here's how mine look like

Postman image

Congratulations! You've successfully created a Node.js application that allows users to upload resumes, extracts relevant information, and stores it in MongoDB.

Here is a link to the github rebo referencing this project

💖 💪 🙅 🚩
fredabod
FredAbod

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