Ad

Confused About Request Path Being Sent To My Express Server From AJAX

- 1 answer

I have a basic setup for Node.js Express server

I am trying to serve html files using res.sendFile, using this route handler:

router.get('/:run_id/:test_num',function(req,res,next){

    var runId = req.params.run_id;
    var testNum = req.params.test_num;

    res.sendFile(path.resolve('results',runId,testNum),{
        maxAge:'58h'
    });

});

in my app.js file, I have this middleware

app.use('/results', require('./routes/results'));

the problem is that the request is being routed like so:

  pathname: '/results/results/1450830953375/test6.txt',
  path: '/results/results/1450830953375/test6.txt',
  href: '/results/results/1450830953375/test6.txt' 

so the problem is that it is /results/results/ instead of just /results

I am using RequireJS and the module that contains the data of the files to be request looks like this:

    define('SumanTestFiles',function(){
            return new Object(["results/1450830340344/test5.txt","results/1450830340344/test6.txt"]);
     });

and then I loop over each file path and request it from the server using jQuery/AJAX:

define(['SumanTestFiles'],function(stf){

    stf.forEach(function (testPath) {
        $.get(testPath).done(function (msg) {...

so you can see that the testPaths have not been tampered with, they without a doubt simply "results/1450830340344/test5.txt", or the like.

so my question is, why does the path show up on my server with two adjacent 'results' string instances in the path?

It does not make a lot of sense, because the request should just be

/results/1450830953375/test6.txt

not

/results/results/1450830953375/test6.txt

anyone have any ideas? thanks

Ad

Answer

This is new to me, since I fairly new to web development

The short answer is that instead of this:

  define('SumanTestFiles',function(){
            return new Object(["results/1450830340344/test5.txt","results/1450830340344/test6.txt"]);
     });

I need to do this

  define('SumanTestFiles',function(){
            return new Object(["/results/1450830340344/test5.txt","/results/1450830340344/test6.txt"]);
     });

otherwise the AJAX request path to my server will be relative to the URL in the searchbar, instead of absolute. Holy crap that was confusing.

Ad
source: stackoverflow.com
Ad