0

My index.html file is like shown below

  <html>
   <body>

    <script src="public/jquery-latest-min.js"></script>
     /*But this script file is not importing and creating error */        

   </body>
 </html>

app.js code is

    var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);
    server.listen(3000);

     app.get('/',function(req, res){
         res.sendfile(__dirname + '/index.html');
     });   

How can the index.html file can serve the jquery file located in public folder.

And my project structure is:

             |-index.html
             |-app.js
             |-public/jquery-latest-min.js
1

2 Answers 2

1

The following can be used to serve static files such as javascript files or CSS files.

var server = express(); // better instead
server.use(express.static(__dirname + '/public'));
Sign up to request clarification or add additional context in comments.

2 Comments

I just added the line server.use(express.static(__dirname + '/public')); but not working
Yes so it should work now. Note that i have used a different variable for express() than yours. Change it accordingly.
0

You can configure static files as per @anubhav suggested.

Then you can reference your files like below.

<script src="jquery-latest-min.js"></script>

You should not use absolute path rather just to reference path which are relative to static folders like public in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.