DEV Community

AKSH DESAI
AKSH DESAI

Posted on

React Resume - uploader Website (Geekyshows)

Backend Code :-

Folder Structure:-

Folder Image

App.js Code:-

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

candidateRoute.js :-

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

CandidateProfile.js :-

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

upload-middleware.js :-

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

candidateProfileController.js :-

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

connectDb.js :-

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

Output Image :-

Output Image

Thank You.
You can follow us on:
Youtube
Instagram

Top comments (0)