-
npm init (to create package.json)
-
npm i express express-fileupload
-
npm i -D nodemon concurrently
-
Add scripts
"scripts": { "start": "node server.js", "server": "nodemon server.js", "client": "npm start --prefix client", "dev" : "concurrently \"npm run server\" \"npm run client\"" },
-
make server.js file and add the following code `const express = require('express') const fileUpload = require('express-fileupload')
const app = express()
app.use(fileUpload())
// Upload Endpoint
app.post('/upload', (req,res)=>{
if(req.files === null){
return res.status(400).json({msg:'No file uploaded'})
}
const file = req.files.file
file.mv(${__dirname}/client/public/uploads/${file.name}
, err=> {
if(err){
console.error(err)
return res.status(500).send(err)
}
res.json({
fileName: file.name, filePath: /uploads/${file.name}
})
})
})
app.listen(5000, ()=> console.log('Server Started..'))`
- create folder name "client" in root directory and create react app inside that
- npx create-react-app
- npm i axios
- add following line to package.json
"proxy": "http://localhost:5000"