I am trying to upload a local file using NodeJs (the file is in the same directory as app.js) to my remote Laravel server but I'm getting null. For some reason the file doesn't seem to get to the server. I'm not sure what I'm doing wrong..
Here is my node app folder structure
node_app/
├── node_modules/
├── app.js
├── screenshot.jpg
├── package.json
Here is my app.js
const fs = require('fs')
const path = require('path')
const axios = require('axios')
const FormData = require('form-data')
const screenshot = require('screenshot-desktop')
let formData = new FormData()
formData.append('screenshot-file', fs.createReadStream(path.join(__dirname, 'screenshot.jpg')))
axios.post(
`https://www.myapp.com/api/upload-screenshot`,
formData,
{ headers: { 'content-type': 'multipart/form-data' } }
)
And here is my endpoint controller
class ScreenshotController extends Controller
{
public function upload(Request $request)
{
// $file is null :(
$file = $request->file('screenshot-file');
$filename = 'screenshot.jpg';
$file->move(storage_path('/uploads/screenshots/'), $filename);
return $filename;
}
}
Error: 'Call to a member function move() on null'
When the request arrives at my controller, there doesn't seem to be a 'screenshot-file' file submitted as it is showing null.
Also using dump($request->all()); it returns an empty array [] which means nothing has been submitted.