Hello. I am creating a small app for a job application, Express backend, Vue frontend. I have to use PUT to write file to system storage.I read a million articles and StackOverflow questions regarding origin/headers/cors/preflight options request. I still can't wrap my head around what is going on with my application. When I send the PUT request, it goes through and writes the file, but in network, my put request is pending for 2-4 minutes and then net::ERR_EMPTY_RESPONSE.Fetch request (this.host = http://localhost:port) //let proxy = 'https://cors-anywhere.herokuapp.com/' let proxy = '' fetch(`${proxy}${this.host}/api/json`, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, method: 'PUT', body: this.jsonstring }).then(res => { res.text() }).then(data => { console.log(data) }).catch(err => { alert(err) console.log('err: ' + err) this.unavailable = true this.uploadsuccessful = false }) Both of my .then() are never fired. App just uploads the file as it should then skips to .catch().Express routerouter.put('/', (req, res) => { let newJsonIndex = 0 let json = req.body const date = new Date() date.toISOString() // readdir to get the number of files in public folder fs.readdir(assetsDir, (err, files) => { newJsonIndex = files.length+1 // writing new json file to public folder fs.writeFile(`${assetsDir}/${newJsonIndex}.json`, JSON.stringify(json, null, 2), (err) => { if (err) { return console.log(err) } // appending date, file name and json file content to input.log in public folder fs.appendFile(`${logsDir}/input.log`, `\r\n${date} ${newJsonIndex}.json ${JSON.stringify(json)}`, (err) => { if(err) { return console.log(err) } console.log("success"); }) }) }) } }) So this creates a new JSON file with req.body and also logs it into new line of input.log. Is there something specific I should do about this route for it to handle that net::ERR_EMPTY_RESPONSE?I'm really at a loss here on what is causing this. Application on the front works as expected, but I want to get rid of any errors...
Submitted July 04, 2020 at 10:17AM by Spxy
No comments:
Post a Comment