I am working on react/node for file uploading. I am not passing files into the API request but passing into the body part of the API request. My react code is like,
import React, { Component } from 'react';
import request from 'superagent';
class App extends Component {
constructor(props) {
super(props);
this.state = {
image1: '',
};
this.handleUploadFile = this.handleUploadFile.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
};
handleUploadFile = (event) => {
console.log("event", event.target);
this.setState({
image1: event.target.files[0],
});
}
handleSubmit(e) {
e.preventDefault();
const dataDemo = {
image: this.state.image1,
};
request
.post(API_URL)
.set('Content-Type', 'application/json')
.send(dataDemo)
.end(function (err, res) {
console.log("err", err);
console.log("res", res);
})
}
render() {
return (
<div>
<form encType="multipart/form-data">
<div style={{width: '100%', marginTop: '10px'}}>
Image 1
<input name="image1" type="file" onChange={this.handleUploadFile} />
</div>
<div style={{width: '100%', marginTop: '10px'}}>
<input type="submit" name="submit" value="submit" onClick={this.handleSubmit} />
</div>
</form>
</div>
)
}`
I want to upload this image into server using Node/Express.js My API code in Node/Express.
So please help me how can I upload this image using API and save into server(inside the folder) using Node/Express.
My Node server code is like,
router.post(END_POINT,function (req, res) {
//I want to add upload code here without any third party module and without req.files/req.file.
})
Please help me. Thank you in advance.
multerand/orbody-parsermodules.