0

I'm setting up a localhost server, and I want to GET photo src through http://localhost:3000/photos/, but It doesn't save photo src to .txt file.

I've already tried in server.js:

app.get('/phofn', function(req, res){
  fs.readFile('Photo.txt', function(err, data){
    res.send(data);
  });
});

app.get('/photos/:poto', function(req, res) {
  var foto = req.src.poto
  fs.appendFile('Photo.txt',foto, function (err) {
    if (err) throw err;
    console.log('Saved!');
  });
  fs.readFile('Photo.txt', function(err, data) {
    res.send(data);
    res.end();
  });
});

And this in HTML:

function loadDoc() {
  var photo = document.getElementById("photo").src;
  $.get("http://localhost:3000/photos/" + photo, function(data, status){
    alert(data);
    console.log(data);
  });
}
1565986223
  • 6,420
  • 2
  • 20
  • 33
Emilis
  • 17
  • 5
  • 2
    `but I can't GET it` - why? is there an error? perhaps relating to CORS? – Jaromanda X Apr 24 '19 at 08:51
  • Odds are this is a duplicate of https://stackoverflow.com/a/35553666/19068 but there's not enough information in the question to be sure – Quentin Apr 24 '19 at 08:52
  • In your HTML, you are declaring the absolute path instead of the relative path `/photos/` as you did in JavaScript. This may be throwing the parser off because it automatically renders your localhost address. – Max Voisard Apr 24 '19 at 08:52
  • @MaxVoisard — That doesn't make any sense. `/photos/` is an absolute *path*. Are you suggesting that the browser will prepend `http://etc/etc` to an absolute URL (i.e. one that starts with the scheme)? Browsers will not do that. – Quentin Apr 24 '19 at 08:55
  • Actually I was referencing more of what you said with the JavaScript and HTML having to have the same paths with the Same Origin Policy. – Max Voisard Apr 24 '19 at 08:59

1 Answers1

0

Looks like you're using express. You've defined a named route paramater app.get('/photos/:poto' ...

poto parameter is accessible through req.params not req.src (unless you're using some middleware to set the src property)

app.get('/photos/:poto', function(req, res) {
  var foto = req.params.poto
  ...
  // rest of the code
}
1565986223
  • 6,420
  • 2
  • 20
  • 33