React Resume - uploader Website (Geekyshows)
Tags: javascript, react, beginners, express
Published: 2022-12-12 12:59:16

## Backend Code :- 



**Folder Structure:-**

![Folder Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eszufnfv87wrellx2uf2.png)

**App.js Code:-**

``` javascript
const express = require("express");
const cors = require("cors");
const connectDb = require("./config/connectDb");
const web = require("./routes/candidateRoute");
const upload = require("./middlewars/upload-middleware");

const app = express();
const PORT = process.env.PORT || 8000;
const DATABASE_URL = "mongodb://localhost://27017";

// CORS Policy 
app.use(cors());

// Connect Database 
connectDb(DATABASE_URL);

// // For Parsing application json 
app.use(express.json());

// Application Level Middleware - For Parsing multipart/ form-data
// app.use(upload.fields([{ name: 'pimage', maxCount: 1 }, { name: 'rdoc', maxCount: 1 }]));

// Load Routes 
app.use("/api", web);



app.listen(PORT, () => {
    console.log(`Server is Listening at ${PORT}.`);
});
```

**candidateRoute.js :-**

``` javascript
const express = require("express");
const candidateProfileController = require("../controllers/candidateProfileController");
const upload = require("../middlewars/upload-middleware");

const router = express.Router();

// Public Routes
router.post("/resume", upload.fields([{ name: 'pimage', maxCount: 1 }, { name: 'rdoc', maxCount: 1 }]), candidateProfileController.saveProfile);

router.get("/", candidateProfileController.profileList);


module.exports = router;
```

**CandidateProfile.js :-**

``` javascript 
const mongoose = require("mongoose");

// Define Schema 
const candidateSchema = new mongoose.Schema({
    name: { type: String, required: true, trim: true },
    email: { type: String, required: true, trim: true },
    dob: { type: Date },
    state: { type: String },
    gender: { type: String },
    location: { type: String },
    pimage: { type: String, required: true },
    rdoc: { type: String, required: true }
});

// Model Schema 
const candidateProfileModel = mongoose.model("CandidateProfile", candidateSchema);

module.exports = candidateProfileModel;
```

**upload-middleware.js :-**

``` javascript
const multer = require("multer");

const upload = multer({
    storage: multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, `public/uploads/${file.fieldname}`);
        },
        filename: function (req, file, cb) {
            cb(null, `${Date.now()}-${file.originalname}`)
        }
    })
})   

module.exports = upload;
```

**candidateProfileController.js :-**

``` javascript
const candidateProfileModel = require("../models/CandidateProfile");

class candidateProfileController {
    static saveProfile = async (req, res) => {
        // console.log('req.body', req.body);
        // console.log('req.files', req.files);

        try {
            const { name, email, dob, state, gender, location } = req.body;

            const pimage = req.files['pimage'][0].filename;
            const rdoc = req.files['rdoc'][0].filename;

            if (name && email && pimage && rdoc) {
                const doc = new candidateProfileModel({
                    name: name,
                    email: email,
                    dob: dob,
                    state: state,
                    gender: gender,
                    location: location,
                    pimage: pimage,
                    rdoc: rdoc
                });
                await doc.save();
                return res.status(201).json({ "status": "success", "message": "Successfully inserted" });
            } else {
                return res.status(200).json({ "status": "failed", "message": "All fiedls are required" });
            };
        } catch (error) {
            return res.status(404).json({ "status": "failed", "message": error.message });
        }

    };

    static profileList = async (req, res) => {
        try {
            const candidate = await candidateProfileModel.find();
            return res.status(200).json({"status": "success", "candidates": candidate});
        } catch (error) {
            return res.status(400).json({"status": "failed", "error": error.message});
        }
    }
}

module.exports = candidateProfileController;
```

**connectDb.js :-**

``` javascript
const mongoose = require("mongoose");

const connectDb = async (DATABASE_URL) => {
    try {
        const dbOptions = {
            dbName: "resumeuploader"
        };
        await mongoose.connect(DATABASE_URL, dbOptions);
        console.log("Db Connection Successfully");
    } catch (error) {
        console.log("error", error.message);
    }
}

module.exports = connectDb;
```

**Output Image :-**

![Output Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85y7z66d3gcieu9frkdr.png)



Thank You.
You can follow us on:
[Youtube ](https://www.youtube.com/c/ULTIMATEPROGRAMMING)
[Instagram](https://www.instagram.com/full_stack_web_developer1/)