How to serve simple static files along with html in NodeJS?How can I upload files asynchronously?How do I include a JavaScript file in another JavaScript file?How to create an HTML button that acts like a link?How can I update NodeJS and NPM to the next versions?How to serve static files in FlaskWhat's the difference between app.use and app.get with express.static?how to access node_module directory which was out side the static content serving folderCode inside get not running after using express.staticHow to --watch uncompressed files (without Webpack)Express File Serving and Static Files

Multi tool use
Multi tool use

Find the 3D region containing the origin bounded by given planes

Why could the Lunar Ascent Engine be used only once?

Can I have a delimited macro with a literal # in the parameter text?

How do we explain the use of a software on a math paper?

Why didn't Daenerys' advisers suggest assassinating Cersei?

Why are stats in Angband written as 18/** instead of 19, 20...?

Why should one apply for UK visa before other visas, on a multi-destination European holiday?

Why were early aviators' trousers flared at the thigh?

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?

What city and town structures are important in a low fantasy medieval world?

How could the B-29 bomber back up under its own power?

Parse a C++14 integer literal

How to safely discharge oneself

In how many ways can we partition a set into smaller subsets so the sum of the numbers in each subset is equal?

In Dutch history two people are referred to as "William III"; are there any more cases where this happens?

Have I found a major security issue with login

Is it possible to view all the attribute data in QGIS

Isn't Kirchhoff's junction law a violation of conservation of charge?

How can sister protect herself from impulse purchases with a credit card?

Does ratifying USMCA imply a (stealth) ratification of UNCLOS?

Working hours and productivity expectations for game artists and programmers

Warped chessboard

Bash Read: Reading comma separated list, last element is missed



How to serve simple static files along with html in NodeJS?


How can I upload files asynchronously?How do I include a JavaScript file in another JavaScript file?How to create an HTML button that acts like a link?How can I update NodeJS and NPM to the next versions?How to serve static files in FlaskWhat's the difference between app.use and app.get with express.static?how to access node_module directory which was out side the static content serving folderCode inside get not running after using express.staticHow to --watch uncompressed files (without Webpack)Express File Serving and Static Files






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static() but it doesn't work.



I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html without the server, it display the styles.



This is my app.js(server):



app.use(express.static('public'));
app.get("/", (req, res)=>
res.sendFile(`$__dirname/index.html`);
);


HTML:



 <head>
<link href="./public/styles.css" rel="stylesheet" type="text/css"/>
<script src="./public/jQuery.js"></script>
<script src="./public/main.js"></script>
</head>


My public folder:



-node_modules
-public
jQuery.js
main.js
styles.css
app.js
index.html
package.json
package-lock.json


Thanks in advance.










share|improve this question






























    1















    I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static() but it doesn't work.



    I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html without the server, it display the styles.



    This is my app.js(server):



    app.use(express.static('public'));
    app.get("/", (req, res)=>
    res.sendFile(`$__dirname/index.html`);
    );


    HTML:



     <head>
    <link href="./public/styles.css" rel="stylesheet" type="text/css"/>
    <script src="./public/jQuery.js"></script>
    <script src="./public/main.js"></script>
    </head>


    My public folder:



    -node_modules
    -public
    jQuery.js
    main.js
    styles.css
    app.js
    index.html
    package.json
    package-lock.json


    Thanks in advance.










    share|improve this question


























      1












      1








      1








      I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static() but it doesn't work.



      I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html without the server, it display the styles.



      This is my app.js(server):



      app.use(express.static('public'));
      app.get("/", (req, res)=>
      res.sendFile(`$__dirname/index.html`);
      );


      HTML:



       <head>
      <link href="./public/styles.css" rel="stylesheet" type="text/css"/>
      <script src="./public/jQuery.js"></script>
      <script src="./public/main.js"></script>
      </head>


      My public folder:



      -node_modules
      -public
      jQuery.js
      main.js
      styles.css
      app.js
      index.html
      package.json
      package-lock.json


      Thanks in advance.










      share|improve this question
















      I'm trying to send an HTML file with its CSS and JS files in NodeJS using express.static() but it doesn't work.



      I've done the same thing as in some videos but I don't know where the problem is. And also when I open the index.html without the server, it display the styles.



      This is my app.js(server):



      app.use(express.static('public'));
      app.get("/", (req, res)=>
      res.sendFile(`$__dirname/index.html`);
      );


      HTML:



       <head>
      <link href="./public/styles.css" rel="stylesheet" type="text/css"/>
      <script src="./public/jQuery.js"></script>
      <script src="./public/main.js"></script>
      </head>


      My public folder:



      -node_modules
      -public
      jQuery.js
      main.js
      styles.css
      app.js
      index.html
      package.json
      package-lock.json


      Thanks in advance.







      javascript html node.js express static-files






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 23 at 18:40









      alditis

      2,76933154




      2,76933154










      asked Mar 23 at 17:49









      Eye PatchEye Patch

      707




      707






















          1 Answer
          1






          active

          oldest

          votes


















          2














          In app.use(express.static('public'));, the string 'public' refers to the directory where the module should search for static files, not the URL path which it should use for the search.



          src="/jQuery.js" will map to the public/jQuery.js file.



          To map URLs starting with /public/ to the public directory, you need to specify that as the first argument to use:



          app.use('/public', express.static('public'));


          And then src="/public/jQuery.js" will map to the public/jQuery.js file.






          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%2f55316675%2fhow-to-serve-simple-static-files-along-with-html-in-nodejs%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            In app.use(express.static('public'));, the string 'public' refers to the directory where the module should search for static files, not the URL path which it should use for the search.



            src="/jQuery.js" will map to the public/jQuery.js file.



            To map URLs starting with /public/ to the public directory, you need to specify that as the first argument to use:



            app.use('/public', express.static('public'));


            And then src="/public/jQuery.js" will map to the public/jQuery.js file.






            share|improve this answer



























              2














              In app.use(express.static('public'));, the string 'public' refers to the directory where the module should search for static files, not the URL path which it should use for the search.



              src="/jQuery.js" will map to the public/jQuery.js file.



              To map URLs starting with /public/ to the public directory, you need to specify that as the first argument to use:



              app.use('/public', express.static('public'));


              And then src="/public/jQuery.js" will map to the public/jQuery.js file.






              share|improve this answer

























                2












                2








                2







                In app.use(express.static('public'));, the string 'public' refers to the directory where the module should search for static files, not the URL path which it should use for the search.



                src="/jQuery.js" will map to the public/jQuery.js file.



                To map URLs starting with /public/ to the public directory, you need to specify that as the first argument to use:



                app.use('/public', express.static('public'));


                And then src="/public/jQuery.js" will map to the public/jQuery.js file.






                share|improve this answer













                In app.use(express.static('public'));, the string 'public' refers to the directory where the module should search for static files, not the URL path which it should use for the search.



                src="/jQuery.js" will map to the public/jQuery.js file.



                To map URLs starting with /public/ to the public directory, you need to specify that as the first argument to use:



                app.use('/public', express.static('public'));


                And then src="/public/jQuery.js" will map to the public/jQuery.js file.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 17:54









                QuentinQuentin

                665k749031065




                665k749031065





























                    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%2f55316675%2fhow-to-serve-simple-static-files-along-with-html-in-nodejs%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







                    Ev0KUP zq UFWOWk6I,nfhwzSd,ePN,12Jn az,JQO S 4Qx 2R0U3D2AiIxhsegsRSTY,dJy3FVo
                    lHebQH vFDP2RZKm L0M0l30LkrE

                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    위키백과:대문 둘러보기 메뉴기부 안내모바일판 대문크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0CebuanoDeutschEnglishEspañolFrançaisItaliano日本語NederlandsPolskiPortuguêsРусскийSvenskaTiếng ViệtWinaray中文العربيةCatalàفارسیSrpskiУкраїнськаБългарскиНохчийнČeštinaDanskEsperantoEuskaraSuomiעבריתMagyarՀայերենBahasa IndonesiaҚазақшаBaso MinangkabauBahasa MelayuBân-lâm-gúNorskRomânăSrpskohrvatskiSlovenčinaTürkçe