nodejs http.createServer read url and remove urlprefix











up vote
0
down vote

favorite
1












Here is what i want:




I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",



and with this url, I can read the file accordingly




Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001



After i added the nginx reverse proxy, I try to write my server code, and here is my http server code



var port = 8001;
var http = require('http');
var fs = require('fs');

var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
var UrlPrefix = "/0000000/abctest"
var hostname = "127.0.0.1"

//var host = hostname+UrlPrefix
var server= http.createServer(function(req,res){
var url = req.url;
console.log(url);
var file = DefaultFileDir + url;

fs.readFile(file, function(err,data){
if(err){
console.log(err);
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404</h1>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
}
res.write(data);
res.end();
}
});

}).listen(port,hostname);
console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");


And the problem come out:
it dont know how to ignore the url prefix "/0000000/abctest"



Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'


However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'



is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.










share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    Here is what i want:




    I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",



    and with this url, I can read the file accordingly




    Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001



    After i added the nginx reverse proxy, I try to write my server code, and here is my http server code



    var port = 8001;
    var http = require('http');
    var fs = require('fs');

    var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
    var UrlPrefix = "/0000000/abctest"
    var hostname = "127.0.0.1"

    //var host = hostname+UrlPrefix
    var server= http.createServer(function(req,res){
    var url = req.url;
    console.log(url);
    var file = DefaultFileDir + url;

    fs.readFile(file, function(err,data){
    if(err){
    console.log(err);
    res.writeHeader(404,{
    'content-type' : 'text/html;charset="utf-8"'
    });
    res.write('<h1>404</h1>');
    res.end();
    }else{
    if(url.substr(url.length-3, 3) == ".gz"){
    res.writeHeader(200,{
    'Content-Encoding' : 'gzip',
    'content-type' : 'application/octet-stream'
    });
    }else{
    res.writeHeader(200,{
    'content-type' : 'text/html;charset="utf-8"'
    });
    }
    res.write(data);
    res.end();
    }
    });

    }).listen(port,hostname);
    console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");


    And the problem come out:
    it dont know how to ignore the url prefix "/0000000/abctest"



    Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'


    However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'



    is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.










    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      Here is what i want:




      I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",



      and with this url, I can read the file accordingly




      Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001



      After i added the nginx reverse proxy, I try to write my server code, and here is my http server code



      var port = 8001;
      var http = require('http');
      var fs = require('fs');

      var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
      var UrlPrefix = "/0000000/abctest"
      var hostname = "127.0.0.1"

      //var host = hostname+UrlPrefix
      var server= http.createServer(function(req,res){
      var url = req.url;
      console.log(url);
      var file = DefaultFileDir + url;

      fs.readFile(file, function(err,data){
      if(err){
      console.log(err);
      res.writeHeader(404,{
      'content-type' : 'text/html;charset="utf-8"'
      });
      res.write('<h1>404</h1>');
      res.end();
      }else{
      if(url.substr(url.length-3, 3) == ".gz"){
      res.writeHeader(200,{
      'Content-Encoding' : 'gzip',
      'content-type' : 'application/octet-stream'
      });
      }else{
      res.writeHeader(200,{
      'content-type' : 'text/html;charset="utf-8"'
      });
      }
      res.write(data);
      res.end();
      }
      });

      }).listen(port,hostname);
      console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");


      And the problem come out:
      it dont know how to ignore the url prefix "/0000000/abctest"



      Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'


      However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'



      is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.










      share|improve this question















      Here is what i want:




      I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",



      and with this url, I can read the file accordingly




      Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001



      After i added the nginx reverse proxy, I try to write my server code, and here is my http server code



      var port = 8001;
      var http = require('http');
      var fs = require('fs');

      var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
      var UrlPrefix = "/0000000/abctest"
      var hostname = "127.0.0.1"

      //var host = hostname+UrlPrefix
      var server= http.createServer(function(req,res){
      var url = req.url;
      console.log(url);
      var file = DefaultFileDir + url;

      fs.readFile(file, function(err,data){
      if(err){
      console.log(err);
      res.writeHeader(404,{
      'content-type' : 'text/html;charset="utf-8"'
      });
      res.write('<h1>404</h1>');
      res.end();
      }else{
      if(url.substr(url.length-3, 3) == ".gz"){
      res.writeHeader(200,{
      'Content-Encoding' : 'gzip',
      'content-type' : 'application/octet-stream'
      });
      }else{
      res.writeHeader(200,{
      'content-type' : 'text/html;charset="utf-8"'
      });
      }
      res.write(data);
      res.end();
      }
      });

      }).listen(port,hostname);
      console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");


      And the problem come out:
      it dont know how to ignore the url prefix "/0000000/abctest"



      Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'


      However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'



      is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.







      node.js nginx reverse-proxy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 5:29









      AIqbalRaj

      9992518




      9992518










      asked Nov 22 at 3:34









      Question-er XDD

      116212




      116212





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423508%2fnodejs-http-createserver-read-url-and-remove-urlprefix%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423508%2fnodejs-http-createserver-read-url-and-remove-urlprefix%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu