Error: ENOENT: no such file or directory, open 'C:\Users\Administrator\Downloads\employee-referral-portal\backend\uploads\1745139875227-RESUME__Ankita.pdf'
is a backend error indicating that your server is trying to open/read the resume file from the folder backend/uploads/ but the file doesn't exist there.
What is happening?
You upload the file from the React frontend via multipart/form-data.
On the backend, the server likely tries to save the file to /uploads folder.
Then, when processing the request or saving referral data, it attempts to read or move the file by opening it from /uploads folder.
The file is missing, causing ENOENT (Error NO ENTry, i.e. file not found).
This triggers a 500 Internal Server Error returned to the frontend.
Common reasons & fixes:
1. Uploads folder does not exist or is not accessible
Ensure the /uploads directory exists in your backend project folder.
The backend should create /uploads if missing, or you should manually create it.
Also, ensure the backend process has write permission there.
2. Multer (or equivalent upload middleware) is not configured correctly
Usually, in an Express backend, file uploads are handled by Multer or similar middleware:
Since posting JSON data directly via Postman to http://localhost:5000/api/referral/submit works fine, but your React form submission with a file uploaded as multipart/form-data throws an error saying the file is missing, this strongly suggests:
Your backend endpoint /api/referral/submithandles JSON requests properly but does not handle multipart/form-data (file uploads) correctly yet.
Why JSON works but file upload fails?
When you send JSON data via Postman (Content-Type: application/json), your backend handles it with something like express.json() middleware and processes the request body easily. No files involved, so no file handling.
When you send multipart/form-data with a file via your React form, the request now contains files — your backend must parse those files and store them (usually via middleware like multer). If multer or similar middleware is not installed and/or not used on that route, the backend won’t parse the files and no file will be saved.
Then, when your backend tries to access the supposed uploaded file in /uploads, it doesn’t exist → ENOENT error.
What you need to do on your Node.js backend:
Install and configure multer middleware (or equivalent) to handle file uploads on that route.
Use multer in the route responsible for handling /api/referral/submit.
Make sure multer stores the uploaded files in your /uploads folder.
Use the multer-provided file info (req.file) in your code.
Sample backend setup with multer for /api/referral/submit
// Set storage engine for multer
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, 'uploads')); // Ensure this folder exists!
},
filename: function (req, file, cb) {
// Create a unique filename with timestamp + original name
const uniqueSuffix = Date.now() + '-' + file.originalname;
cb(null, uniqueSuffix);
}
});
const upload = multer({ storage });
// Route that handles referral submit with file upload
app.post('/api/referral/submit', upload.single('resume'), (req, res) => {
try {
// req.file contains the uploaded file info
console.log('Received file:', req.file);
code
// `req.body` contains other fields (candidateName, email, referredBy, etc.)
console.log('Other form data:', req.body);
// Your logic to store referral data, etc.
res.json({ message: 'Referral submitted successfully!' });
// Start your server, etc... in which file i have to add this ..complete upaded provide plz
Assistant
You should add this code (or a version of it) to your backend server code, which usually resides in a file like server.js or app.js — the main entry point for your Express backend.
Here's a complete minimal example backend setup with your upload route, including necessary pieces:
js
// backend/app.js (or server.js)
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = 5000;
// Make sure uploads directory exists, or create it
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
// Middleware to parse JSON body for non-file requests
app.use(express.json());
// Multer storage config
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, uploadsDir);
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + file.originalname;
cb(null, uniqueSuffix);
}
});
const upload = multer({ storage });
// Route to handle referral submission with file upload
app.post('/api/referral/submit', upload.single('resume'), (req, res) => {
try {
console.log('Uploaded file info:', req.file); // Uploaded file info
console.log('Form fields:', req.body); // Other form data
// You can, for example, save the form data + filename to your DB here
res.json({ message: 'Referral submitted successfully!' });
} catch (error) {
console.error('Error in referral submission:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
// Start server
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(Server running on port ${PORT}));
this is my servr.js
Assistant
Thanks for sharing your server.js! Since you already have a clean separation with routes (referralRoutes), the best way to integrate file upload handling is to:
Create a uploads folder (if it doesn't exist)
Add multer setup and upload handling inside your referralRoutes route handler (not in server.js)
Keep server.js as is (just ensure static serving of /uploads folder is enabled, which you already do)
Step-by-step instructions:
1. Ensure /uploads exists
In your backend root (where server.js is), create an uploads folder manually, or add this small snippet in server.js before listeners:
2. Update your referralRoutes to handle file upload
Inside your backend folder, you should have a file for routes: routes/referralRoutes.js
Edit/create it like this:
js
// routes/referralRoutes.js
const express = require('express');
const multer = require('multer');
const path = require('path');
const router = express.Router();
// Configure multer storage
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, '..', 'uploads')); // ../ to go up one folder from routes/
},
filename: function (req, file, cb) {
const uniqueName = Date.now() + '-' + file.originalname;
cb(null, uniqueName);
}
});
const upload = multer({ storage });
// Your referral submit route with multer middleware
router.post('/submit', upload.single('resume'), (req, res) => {
try {
console.log('Uploaded file:', req.file);
console.log('Form data:', req.body);
// TODO: save referral data (req.body) + file info (req.file.filename) to DB
res.status(200).json({ message: 'Referral submitted successfully!' });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal server error' });
}
});
module.exports = router;
User
probid the upatde server.js file for removing the error
Assistant
Certainly! Here's your updated server.js file with the minimal code added to ensure the uploads folder exists and the rest of your existing setup maintained to avoid the ENOENT error you got:
js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const referralRoutes = require('./routes/referralRoutes');
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
// Ensure 'uploads' folder exists
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
// Serve static files from uploads folder so files are accessible if needed
app.use('/uploads', express.static('uploads'));
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => console.log("Connected to MongoDB"))
.catch(err => console.log(err));
app.use('/api/referral', referralRoutes);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
What changed / added?
Added these lines to create the /uploads folder automatically if missing:
Make sure your /routes/referralRoutes.js file implements the multer middleware to save the uploaded file in this /uploads folder (see my previous message for example code on that).
Quick checklist:
Your backend root folder contains /uploads after this runs (auto-created if not exists)
Your frontend sends file field as resume via multipart/form-data
Your referral route uses upload.single('resume') to parse/save file
After this update, the ENOENT file missing error on the server should be resolved because:
The folder where multer saves files is guaranteed to exist
Multer can write the uploaded files there successfully
If you want I can help you patch referralRoutes.js as well for complete working example — just say!
User
Error during referral submission: TypeError: Cannot destructure property 'candidateName' of 'req.body' as it is undefined.
at sendReferral (C:\Users\Administrator\Downloads\employee-referral-portal\backend\controllers\referralController.js:6:13)
at Layer.handleRequest (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\lib\layer.js:152:17)
at next (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\lib\route.js:157:13)
at Route.dispatch (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\lib\route.js:117:3)
at handle (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\index.js:435:11)
at Layer.handleRequest (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\lib\layer.js:152:17)
at C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\index.js:295:15
at processParams (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\index.js:582:12)
at next (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\index.js:291:5)
at Function.handle (C:\Users\Administrator\Downloads\employee-referral-portal\backend\node_modules\router\index.js:186:3)
what this error means
Assistant
The error message:
code
TypeError: Cannot destructure property 'candidateName' of 'req.body' as it is undefined.
at sendReferral (C:\...\referralController.js:6:13)
means in your controller function sendReferral, you're trying to read properties from req.body, but req.body is undefined at that moment.
What does this mean?
req.body is undefined, so your code like:
js
const { candidateName, email, ... } = req.body;
throws an error because you can't destructure properties from undefined.
Why would req.body be undefined here?
This usually happens when:
The request body is not parsed correctly by middleware before reaching your controller.
You are using multipart/form-data form data with file upload (like with multer), and you do not have middleware correctly set up to parse that, or you are accessing req.body before multer processes it.
You might be sending the POST request incorrectly from frontend or Postman.
Common causes in your scenario
Since you are submitting a form with file upload (multipart/form-data):
You must use multer middleware on the route before your controller function, so multer can parse the multipart/form-data request, process the file, and populate req.body and req.file.
module.exports = router;
this is my referrral routes
Assistant
Thanks for sharing your referralRoutes.js.
The problem is that you are not using multer middleware on this route, so the multipart/form-data body with the file cannot be parsed, and req.body remains undefined.
What you need to do:
Import and configure multer in your referralRoutes and add upload.single('resume') middleware before the controller sendReferral.
module.exports = { sendReferral };
thi sis sendrefrral make the chnges int his
Assistant
Thanks for sharing your sendReferral controller!
The issue is, as explained before, that referredBy is currently a stringified JSON coming from the frontend (JSON.stringify in the form data), so destructuring it directly from req.body causes troubles and consequently Mongoose validation fails.
Here is your corrected sendReferral controller with comments and fixes:
can u make it look good ..if i provide u referalform.js amd css file
Assistant
Absolutely! Please provide your ReferralForm.jsx and associated CSS or styling info, and I’ll help improve the UI/UX and clean up the code styling for you. Feel free to share the components and any particular style preferences or requirements you have as well.
User
import React, { useState } from 'react';
import {
Box,
TextField,
Button,
Typography,
Paper,
Alert,
Grid,
} from '@mui/material';
import axios from 'axios';
Thanks for sharing your ReferralForm.jsx! It's already pretty well structured and uses MUI nicely.
Here’s an updated version with some improvements to the UI/UX and code comments — plus subtle styling enhancements to make it look cleaner and more professional:
Updated & polished ReferralForm.jsx
jsx
User
i want to make some chnges ...i wanted the form to take name , email ,mobile no, applyiling for --which is a dropdown having -- leg , non-legal and othe--a textbox , relarionship -- dropdown with friend family , acquittance, other , work exp - having dropdown -- inetrn , fresher, expreinece, resume - optional , referd by -- name , email
Assistant
Got it! You want to update the referral form with the following fields and input types:
Field
Type
Notes
Name
TextField
Required
Email
Email TextField
Required
Mobile No
TextField
Required
Applying For
Dropdown
Options: Legal, Non-Legal, Other (if Other, show textbox)
Relationship
Dropdown
Options: Friend, Family, Acquaintance, Other (if Other, show textbox)
Work Experience
Dropdown
Options: Intern, Fresher, Experienced
Resume
File upload
Optional
Referred By
Name, Email
Both TextFields, Required
I'll update the form accordingly, using MUI components. You'll get conditional inputs for "Other" options.
Updated ReferralForm.jsx with your requirements
jsx
User
in what other filesi i need yo maje the chnges for final pouctio ready code
Assistant
Great question! To make your referral form feature production-ready with the updated fields and functionality, here is a list of the other files and parts of your backend/frontend you will likely need to update or verify:
1. Backend Model (Mongoose Schema)
Update your Referral model (models/Referral.js or wherever your Mongoose schema is defined) to include the new fields:
2. Backend Controller (sendReferral in controllers/referralController.js)
Update your controller to handle and validate the new fields (relationship, workExp, and your adjusted field names).
Make sure you parse multipart/form-data (already done with multer) and JSON.parsereferredBy.
Not yet included in a published release.
app.post('/api/referral/submit', upload.single('resume'), (req, res) => {
// req.file contains the uploaded file info
// req.body will have other form data
// Save data, etc.
});
3. Your backend code tries to access the file but the file has not been saved yet or filename used is wrong
Make sure your backend is using req.file.path (provided by multer) to access the saved file.
Don't manually construct a file path using the form filename because multer renames files (usually).
If your backend tries to open the file using some timestamped filename (1745139875227-RESUME__Ankita.pdf), confirm that multer saved it with exactly that name.
Log req.file in the backend to check the path.
What you should do step-by-step:
Check your backend /uploads folder exists.
Check your backend upload middleware (multer or similar) configuration.
Add console logs in backend /api/referral/submit endpoint to print out:
Make sure you access the file using req.file.path or multer's path, not constructing path from client-sent filename.
If your intention is just to store the file and link it, ensure the saving process matches the usage.
Consider adding error handling for missing folders or permission issues.
Additional notes regarding your React code:
Your front-end ReferralForm.jsx looks mostly fine in how it sends the form data and file as multipart. No changes needed here.
Summary:
The main problem is on your backend: the uploads folder/file is missing or incorrectly handled. Create the uploads directory and confirm your multer or upload middleware is properly saving files so your backend can find them.
If you want, I can help verify or write an example of the backend file upload handler — just let me know!
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
// JSON middleware (for JSON data)
app.use(express.json());
// Set storage engine for multer
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, 'uploads')); // Ensure this folder exists!
},
filename: function (req, file, cb) {
// Create a unique filename with timestamp + original name
const uniqueSuffix = Date.now() + '-' + file.originalname;
cb(null, uniqueSuffix);
}
});
const upload = multer({ storage });
// Route that handles referral submit with file upload
app.post('/api/referral/submit', upload.single('resume'), (req, res) => {
try {
// `req.file` contains the uploaded file info
console.log('Received file:', req.file);
// `req.body` contains other fields (candidateName, email, referredBy, etc.)
console.log('Other form data:', req.body);
// Your logic to store referral data, etc.
res.json({ message: 'Referral submitted successfully!' });
} catch (error) {
console.error('Error handling referral submit:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
// Start your server, etc...
Additional tips:
Ensure the uploads folder exists in your backend directory (mkdir uploads if not).
Make sure your backend has write permissions to that folder.
Make sure React’s field name for the file is "resume" — matches upload.single('resume').
If you want to handle multiple files, you can use upload.array or upload.fields, but for your form one file input resume is correct.
If you previously only had app.use(express.json()) for JSON request parsing, note it does NOT handle multipart/form-data — multer is needed.
Summary:
Your frontend is correctly submitting multipart/form-data with a file; your backend must use multer middleware (or similar) properly to handle and save the incoming file, otherwise the file won’t be saved and an error like ENOENT will occur when your backend tries to access it.
If you want, share some backend code so I can help integrate multer or fix your upload handler!
Steps for integration with your existing backend:
Find your backend entry point file (commonly app.js or server.js inside backend folder).
Add the multer dependency if you haven't:
bash
npm install multer
Add the code above or integrate it into your existing Express setup:
Where your express() app is created.
Make sure you create /uploads folder or programmatically as shown.
Replace your /api/referral/submit route code with the one that has upload.single('resume') middleware.
Save and restart your backend server, e.g.:
bash
node app.js
# or if you use nodemon
nodemon app.js
Make sure your React frontend matches the field name resume when uploading the file – which your current React code already does.
Recap:
The above code creates an Express server that handles:
JSON data (express.json() middleware)
Multipart form with a resume upload (multer)
Saves files in uploads folder
Logs info for debugging
Responds with success message
You basically either replace or update your current app.js or server.js file with this logic.
If you want I can help you adapt this to your existing backend codebase if you share that!
Bonus: Folder structure example
code
backend/
├── app.js <-- Your Express server here
├── uploads/ <-- Multer saves uploaded files here
├── package.json
└── node_modules/
frontend/
├── src/
│ └── ReferralForm.jsx
└── package.json
Let me know if you want me to help with your existing backend code based on your current structure!
3. Verify in server.js:
You already have
js
app.use('/uploads', express.static('uploads'));
which is perfect. This serves uploaded files statically.
4. Restart your backend:
Make sure you restart your server after these changes:
bash
node server.js
# or
nodemon server.js
5. Frontend
Your React code already sends the file with field name "resume", so this will be caught by upload.single('resume').
Summary:
Server static serves /uploads
Multer saves uploaded file in /uploads
Your route logs files and request body, no more file not found error
Referral data and uploaded file info are now both available in backend for further handling (e.g. saving to MongoDB)
If you want more help on saving the referral data + file path to MongoDB model, just ask!