Express serves static files that are not in root directory












-1















I have a website with a lot of HD videos so I want to put the videos files outside of web root directory.



Here is my web root directory:



/var/node/myapp


For some basic static files like javascript, css... I put them in public directory.



/var/node/myapp/public


For video files I want to put here



/hdd/videos


This is my current serve static code:



app.use(serveStatic(path.join(__dirname, 'public'), {
maxAge: keys.conf.maxAge,
etag: true,
setHeaders: setCustomCacheControl
}));
function setCustomCacheControl (res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=0')
}
}









share|improve this question





























    -1















    I have a website with a lot of HD videos so I want to put the videos files outside of web root directory.



    Here is my web root directory:



    /var/node/myapp


    For some basic static files like javascript, css... I put them in public directory.



    /var/node/myapp/public


    For video files I want to put here



    /hdd/videos


    This is my current serve static code:



    app.use(serveStatic(path.join(__dirname, 'public'), {
    maxAge: keys.conf.maxAge,
    etag: true,
    setHeaders: setCustomCacheControl
    }));
    function setCustomCacheControl (res, path) {
    if (serveStatic.mime.lookup(path) === 'text/html') {
    res.setHeader('Cache-Control', 'public, max-age=0')
    }
    }









    share|improve this question



























      -1












      -1








      -1








      I have a website with a lot of HD videos so I want to put the videos files outside of web root directory.



      Here is my web root directory:



      /var/node/myapp


      For some basic static files like javascript, css... I put them in public directory.



      /var/node/myapp/public


      For video files I want to put here



      /hdd/videos


      This is my current serve static code:



      app.use(serveStatic(path.join(__dirname, 'public'), {
      maxAge: keys.conf.maxAge,
      etag: true,
      setHeaders: setCustomCacheControl
      }));
      function setCustomCacheControl (res, path) {
      if (serveStatic.mime.lookup(path) === 'text/html') {
      res.setHeader('Cache-Control', 'public, max-age=0')
      }
      }









      share|improve this question
















      I have a website with a lot of HD videos so I want to put the videos files outside of web root directory.



      Here is my web root directory:



      /var/node/myapp


      For some basic static files like javascript, css... I put them in public directory.



      /var/node/myapp/public


      For video files I want to put here



      /hdd/videos


      This is my current serve static code:



      app.use(serveStatic(path.join(__dirname, 'public'), {
      maxAge: keys.conf.maxAge,
      etag: true,
      setHeaders: setCustomCacheControl
      }));
      function setCustomCacheControl (res, path) {
      if (serveStatic.mime.lookup(path) === 'text/html') {
      res.setHeader('Cache-Control', 'public, max-age=0')
      }
      }






      javascript node.js express






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 4:25









      Foo

      1




      1










      asked Nov 18 '18 at 2:37









      Vuong TranVuong Tran

      1614




      1614
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can set multiple static directories. Example:



          app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
          app.use(express.static('/hdd/videos'));


          However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.



          Document for express static file in here






          share|improve this answer


























          • This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

            – Shawn Andrews
            Nov 18 '18 at 18:28











          • You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

            – KibGzr
            Nov 18 '18 at 23:52













          • May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

            – KibGzr
            Nov 19 '18 at 0:01



















          -1














          If you want to have a directory accessed outside of the root web server you'll need to go up a directory level via ... You didn't specify where exactly /hdd/videos is in relation to your root directory, but it should change to look something like this:



          var videosDirectory = __dirname + '/../../hdd/videos';
          app.use(serveStatic(videosDirectory, {
          maxAge: keys.conf.maxAge,
          etag: true,
          setHeaders: setCustomCacheControl
          }));
          function setCustomCacheControl (res, path) {
          if (serveStatic.mime.lookup(path) === 'text/html') {
          res.setHeader('Cache-Control', 'public, max-age=0')
          }
          }





          share|improve this answer
























          • I want to keep the public directory to serve css files. Please show me how

            – Vuong Tran
            Nov 18 '18 at 2:56











          • Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

            – Shawn Andrews
            Nov 18 '18 at 18:26



















          -1














          You can create a symlink under /var/node/myapp/public to point to /hdd/videos:



          On Linux/Unix/OSX, for example:



          ln -s /hdd/videos /var/node/myapp/public/videos


          This way you don't expose your entire root directory, and you can separate where you store the actual videos from where you serve them without copying or moving them when they're added/removed/etc.



          When the user hits your route /videos it will look for it under /var/node/myapp/public as per your static route. It will see videos as a link and follow that link to /hdd/videos where the videos will be available.






          share|improve this answer

























            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',
            autoActivateHeartbeat: false,
            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%2f53357402%2fexpress-serves-static-files-that-are-not-in-root-directory%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can set multiple static directories. Example:



            app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
            app.use(express.static('/hdd/videos'));


            However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.



            Document for express static file in here






            share|improve this answer


























            • This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

              – Shawn Andrews
              Nov 18 '18 at 18:28











            • You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

              – KibGzr
              Nov 18 '18 at 23:52













            • May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

              – KibGzr
              Nov 19 '18 at 0:01
















            1














            You can set multiple static directories. Example:



            app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
            app.use(express.static('/hdd/videos'));


            However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.



            Document for express static file in here






            share|improve this answer


























            • This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

              – Shawn Andrews
              Nov 18 '18 at 18:28











            • You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

              – KibGzr
              Nov 18 '18 at 23:52













            • May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

              – KibGzr
              Nov 19 '18 at 0:01














            1












            1








            1







            You can set multiple static directories. Example:



            app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
            app.use(express.static('/hdd/videos'));


            However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.



            Document for express static file in here






            share|improve this answer















            You can set multiple static directories. Example:



            app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
            app.use(express.static('/hdd/videos'));


            However, the path that you provide to the express.static function is relative to the directory from where you launch your node process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.



            Document for express static file in here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 18 '18 at 8:05

























            answered Nov 18 '18 at 7:55









            KibGzrKibGzr

            1,466610




            1,466610













            • This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

              – Shawn Andrews
              Nov 18 '18 at 18:28











            • You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

              – KibGzr
              Nov 18 '18 at 23:52













            • May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

              – KibGzr
              Nov 19 '18 at 0:01



















            • This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

              – Shawn Andrews
              Nov 18 '18 at 18:28











            • You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

              – KibGzr
              Nov 18 '18 at 23:52













            • May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

              – KibGzr
              Nov 19 '18 at 0:01

















            This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

            – Shawn Andrews
            Nov 18 '18 at 18:28





            This fails to answer the asker's problem of pointing nodejs to a directory outside of the node process. In your example, the second line will route inside the node process directory to /var/node/myapp/hdd/videos instead of outside the root directory to ..somewhere outside root../hdd/videos

            – Shawn Andrews
            Nov 18 '18 at 18:28













            You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

            – KibGzr
            Nov 18 '18 at 23:52







            You’re wrong! I said that “If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve.” I don’t know which is absolute path, just example :))

            – KibGzr
            Nov 18 '18 at 23:52















            May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

            – KibGzr
            Nov 19 '18 at 0:01





            May be /root/home/hdd/videos or anything else. Don’t copy, paste then run without modify my code.

            – KibGzr
            Nov 19 '18 at 0:01













            -1














            If you want to have a directory accessed outside of the root web server you'll need to go up a directory level via ... You didn't specify where exactly /hdd/videos is in relation to your root directory, but it should change to look something like this:



            var videosDirectory = __dirname + '/../../hdd/videos';
            app.use(serveStatic(videosDirectory, {
            maxAge: keys.conf.maxAge,
            etag: true,
            setHeaders: setCustomCacheControl
            }));
            function setCustomCacheControl (res, path) {
            if (serveStatic.mime.lookup(path) === 'text/html') {
            res.setHeader('Cache-Control', 'public, max-age=0')
            }
            }





            share|improve this answer
























            • I want to keep the public directory to serve css files. Please show me how

              – Vuong Tran
              Nov 18 '18 at 2:56











            • Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

              – Shawn Andrews
              Nov 18 '18 at 18:26
















            -1














            If you want to have a directory accessed outside of the root web server you'll need to go up a directory level via ... You didn't specify where exactly /hdd/videos is in relation to your root directory, but it should change to look something like this:



            var videosDirectory = __dirname + '/../../hdd/videos';
            app.use(serveStatic(videosDirectory, {
            maxAge: keys.conf.maxAge,
            etag: true,
            setHeaders: setCustomCacheControl
            }));
            function setCustomCacheControl (res, path) {
            if (serveStatic.mime.lookup(path) === 'text/html') {
            res.setHeader('Cache-Control', 'public, max-age=0')
            }
            }





            share|improve this answer
























            • I want to keep the public directory to serve css files. Please show me how

              – Vuong Tran
              Nov 18 '18 at 2:56











            • Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

              – Shawn Andrews
              Nov 18 '18 at 18:26














            -1












            -1








            -1







            If you want to have a directory accessed outside of the root web server you'll need to go up a directory level via ... You didn't specify where exactly /hdd/videos is in relation to your root directory, but it should change to look something like this:



            var videosDirectory = __dirname + '/../../hdd/videos';
            app.use(serveStatic(videosDirectory, {
            maxAge: keys.conf.maxAge,
            etag: true,
            setHeaders: setCustomCacheControl
            }));
            function setCustomCacheControl (res, path) {
            if (serveStatic.mime.lookup(path) === 'text/html') {
            res.setHeader('Cache-Control', 'public, max-age=0')
            }
            }





            share|improve this answer













            If you want to have a directory accessed outside of the root web server you'll need to go up a directory level via ... You didn't specify where exactly /hdd/videos is in relation to your root directory, but it should change to look something like this:



            var videosDirectory = __dirname + '/../../hdd/videos';
            app.use(serveStatic(videosDirectory, {
            maxAge: keys.conf.maxAge,
            etag: true,
            setHeaders: setCustomCacheControl
            }));
            function setCustomCacheControl (res, path) {
            if (serveStatic.mime.lookup(path) === 'text/html') {
            res.setHeader('Cache-Control', 'public, max-age=0')
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 18 '18 at 2:46









            Shawn AndrewsShawn Andrews

            945617




            945617













            • I want to keep the public directory to serve css files. Please show me how

              – Vuong Tran
              Nov 18 '18 at 2:56











            • Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

              – Shawn Andrews
              Nov 18 '18 at 18:26



















            • I want to keep the public directory to serve css files. Please show me how

              – Vuong Tran
              Nov 18 '18 at 2:56











            • Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

              – Shawn Andrews
              Nov 18 '18 at 18:26

















            I want to keep the public directory to serve css files. Please show me how

            – Vuong Tran
            Nov 18 '18 at 2:56





            I want to keep the public directory to serve css files. Please show me how

            – Vuong Tran
            Nov 18 '18 at 2:56













            Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

            – Shawn Andrews
            Nov 18 '18 at 18:26





            Then keep your existing public directory app.use() function, but before that function insert a app.use(/videos, express.static(videosDirectory)). Now you can make calls to /vidoes/birdvideo and it will internally route to /hdd/videos/birdvideo

            – Shawn Andrews
            Nov 18 '18 at 18:26











            -1














            You can create a symlink under /var/node/myapp/public to point to /hdd/videos:



            On Linux/Unix/OSX, for example:



            ln -s /hdd/videos /var/node/myapp/public/videos


            This way you don't expose your entire root directory, and you can separate where you store the actual videos from where you serve them without copying or moving them when they're added/removed/etc.



            When the user hits your route /videos it will look for it under /var/node/myapp/public as per your static route. It will see videos as a link and follow that link to /hdd/videos where the videos will be available.






            share|improve this answer






























              -1














              You can create a symlink under /var/node/myapp/public to point to /hdd/videos:



              On Linux/Unix/OSX, for example:



              ln -s /hdd/videos /var/node/myapp/public/videos


              This way you don't expose your entire root directory, and you can separate where you store the actual videos from where you serve them without copying or moving them when they're added/removed/etc.



              When the user hits your route /videos it will look for it under /var/node/myapp/public as per your static route. It will see videos as a link and follow that link to /hdd/videos where the videos will be available.






              share|improve this answer




























                -1












                -1








                -1







                You can create a symlink under /var/node/myapp/public to point to /hdd/videos:



                On Linux/Unix/OSX, for example:



                ln -s /hdd/videos /var/node/myapp/public/videos


                This way you don't expose your entire root directory, and you can separate where you store the actual videos from where you serve them without copying or moving them when they're added/removed/etc.



                When the user hits your route /videos it will look for it under /var/node/myapp/public as per your static route. It will see videos as a link and follow that link to /hdd/videos where the videos will be available.






                share|improve this answer















                You can create a symlink under /var/node/myapp/public to point to /hdd/videos:



                On Linux/Unix/OSX, for example:



                ln -s /hdd/videos /var/node/myapp/public/videos


                This way you don't expose your entire root directory, and you can separate where you store the actual videos from where you serve them without copying or moving them when they're added/removed/etc.



                When the user hits your route /videos it will look for it under /var/node/myapp/public as per your static route. It will see videos as a link and follow that link to /hdd/videos where the videos will be available.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 18 '18 at 4:35

























                answered Nov 18 '18 at 4:30









                DerekDerek

                3,20821428




                3,20821428






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357402%2fexpress-serves-static-files-that-are-not-in-root-directory%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)