1

I want to reply to Fetch request with a URL in string format. Examples I've seen look fairly easy, but I believe my cors middleware may be messing something up.

Here is my server:

const express = require('express');
const cors = require('cors');
const db = require('./database/db');
const app = express();

app.use(cors());
app.use(require('./router.img.js'));
app.listen(4000, console.log('Listening on 4000'));

module.exports = app;

Here's where I send the response:

exports.postImage = (req, res) => {
  let sliceIndex = req.file.originalname.indexOf('.');
  let fileType = req.file.originalname.slice(sliceIndex);
  let url = "https://instaimages.sfo2.digitaloceanspaces.com/" + 
  req.body.number + fileType;

  res.set('Content-Type', 'text/html'); 
  res.send(url);
};

I see a response on the front-end, but it does not contain the URL string. Here is what it looks like:

Response {type: "cors", url: "http://localhost:4000/upload/", 
redirected: false, status: 200, ok: true, …}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
David
  • 107
  • 1
  • 7

1 Answers1

3

Try

fetch('/your/api/endpoint')
  .then(res => res.text())
  .then(text => console.log(text));

fetch actually resolves to a Response object from which you need to do an additional step to extract the data you need. More information here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

miyu
  • 346
  • 1
  • 8
  • YES, thank you so much. Looks like I need to read more of Fetch's docs, I could've sworn it was an issue with my server. – David Mar 24 '19 at 19:05