Question: An error occurred while submitting the form. WebSocket connection to ' ws: / / localhost: 3 0 0 0 / ws ' failed: WebSocketClient @
An error occurred while submitting the form.
WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
partonrender.comapigear:
Failed to load resource: the server responded with a status of
bundle.js:WebSocket connection to ws:localhost:ws failed:
WebSocketClient @ bundle.js:
bundle.js:WebSocket connection to ws:localhost:ws failed:
Running the below code: Please help. Thank you.
const express requireexpress;
const mongoose requiremongoose;
const multer requiremulter;
const path requirepath;
const cors requirecors;
const app express;
Middleware
app.useexpressjson;
app.usecors;
Connect to MongoDB
mongoose
connectmongodb:localhost:itemsDB
useNewUrlParser: true,
useUnifiedTopology: true,
then console.logConnected to MongoDB'
catcherr console.errorError connecting to MongoDB: err;
Define Item Schema
const itemSchema new mongoose.Schema
name: type: String, required: true
brand: type: String, required: true
price: type: Number, required: true
image: type: String, required: true
;
const Item mongoose.modelItem itemSchema;
Set up multer for file uploads
const storage multer.diskStorage
destination: req file, cb cbnull 'uploads
filename: req file, cb cbnull Date.now path.extnamefileoriginalname
;
const upload multer storage ;
Serve uploaded images
app.useuploads express.staticuploads;
Routes
Create POST
app.postitems upload.singleimage async req res
try
const name, brand, price req.body;
if req.file
return res.statusjson success: false, message: 'Image is required' ;
const newItem new Item
name,
brand,
price: parseFloatprice
image: req.file.filename,
;
await newItem.save;
res.statusjson success: true, newItem ;
catch err
res.statusjson success: false, message: err.message ;
;
Update PUT
app.putitems:id upload.singleimage async req res
try
const name, brand, price req.body;
const updateData name, brand, price: parseFloatprice;
if reqfile
updateData.image req.file.filename;
const updatedItem await Item.findByIdAndUpdatereqparams.id updateData, new: true ;
if updatedItem
return res.statusjson success: false, message: 'Item not found' ;
res.json success: true, updatedItem ;
catch err
res.statusjson success: false, message: err.message ;
;
Delete DELETE
app.deleteitems:id async req res
try
const deletedItem await Item.findByIdAndDeletereqparams.id;
if deletedItem
return res.statusjson success: false, message: 'Item not found' ;
res.json success: true ;
catch err
res.statusjson success: false, message: err.message ;
;
Start the server
app.listen console.logServer running on port ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
