I am making a text editor for my blog (Using React) and using CKEditor for it. I am using an express server on a windows machine to handle the image upload request. When I browse the uploads directory on the windows machine, the file is present but on the CKEditor page I get the following error:
This is the CKEditor component code (using react):
<CKEditor
editor={ClassicEditor}
data='<p>Hello World</p>'
onChange={(event,editor) => {
setHtml(editor.getData());
console.log(html)
}}
config={
{
ckfinder: {
uploadUrl:'http://localhost:8000/upload'
}
}
}
/>
This is the server code (Using express):
const express = require('express');
const PORT = 8000;
const app = express();
const bodyparser = require('body-parser'); //Body parsing middle ware
const morgan = require('morgan'); //HTTP request logger middle ware
const multipart = require('connect-multiparty');
const MultipartMiddleWare = multipart({uploadDir:'./uploads'});
const cors = require('cors'); // Middle ware to handle cors
app.use(cors())
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
app.get('/', (req, res) => {
res.status(200).json({
message: "Test MSG"
})
})
app.post('/upload',MultipartMiddleWare,(req,res) => {
res.send("Success");
console.log(req.files.upload)
})
app.listen(PORT, console.log(`server has successfully startet at PORT: ${PORT}`))
The correct response for uploaded image is
{
uploaded: true,
url: 'path to uploaded image'
}
so in your app.post use the following code:
app.post('/upload',MultipartMiddleWare,(req,res) => {
let tempFile = req.files.upload;
let path = tempFile.path;
res.status(200).json({
uploaded: true,
url: `http://localhost:8000/${path}`
})
})