Building A Resume Upload And Parsing App With NodeJs And MongoDb
Tags: javascript, beginners, tutorial, node
Published: 2024-02-02 11:42:14

## Lets Take A DIVE IN

![Diving Gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62aq12xo2iehzcb0shvd.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](https://nodejs.org/).

```bash
npm init -y
npm install express multer pdf-parse mongoose
```
## Project Setup
1. Create an `app.js` file for your Node.js application.
2. Set up Express, Multer, and MongoDB connection in `app.js`.

```javascript
// 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

```javascript
// 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.

```javascript
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.
```javascript
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

![Postman image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0gnwrfeohg269to4qnx.png)

> 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](https://github.com/FredAbod/Parsing-Pdf)