1

I try to load a file with node.js.

In my view, I've got a button :

doctype 5
html(ng-app="lineApp")
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    p filename: #{filename}
    button(onclick="location.href='/app/#{filename}'") click me

The page display a paragraph with filename: C:\users\username\my filename.txt. When I click on the button, the URL is something like http://localhost:8080/app/C:usersusernamemy%20filename.txt

So when I try to retrieve the parameter

exports.appli = function (req, res) {
    var filename = req.params.filename;
    //....
    });
};

with the server side call :

app.get('/app/:filename?', routes.appli);

I got an invalid filename. My question is then, how to pass a file path as a parameter in URL ?

GuillaumeA
  • 3,493
  • 4
  • 35
  • 69
  • As typed in the question, that would show up in `req.params` as `C:usersusernamemy filename.txt` which isn't a vaild windows path. Is there a typo in the question? Or is the path legitimately wrong? – Matthew Bakaitis Jun 17 '14 at 20:36
  • Just edited the post to clarify. The variable #{filename} is a valid windows path, but when it's passed as a parameter, I loose the special characters. There is something to deal with escaping characters, but I don't know how... – GuillaumeA Jun 17 '14 at 20:41

1 Answers1

1

This is a problem with the slashes acting as escape characters.

The first time you pass the string to the client, any escaped slashes (ex: c:\\users\\username\\my file.txt) are converted to single slashes.

When you use href.location, the slashes act as escape characters a second time...which is why they drop out when you try to call the server using it.

You could:

  1. Create two variables to pass to the jade template, one the filename as-is and the other an HTML encoded string
  2. pass the variables to the jade template:

For example, based upon your original jade:

body
    p filename: #{filename}
    button(onclick="location.href='/app/#{encodedFilename}'") click me
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53