Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading local jquery.js file in node js

I want to load local jquery file in my node js chat application.I have searched a lot but i cant find a proper solution.

<!doctype html>
<html>
  <head>
    <title>Socketio</title>
  </head>
  <body>
      <!--This wont work -->
      <script src="/socket.io/jquery.min.js"></script> 
        <!--This will work -->
      <script src="/socket.io/socket.io.js"></script>

  </body>
</html>

I just copy jquery.js file to socket.io folder.The socket.io.js file loads properly but jquery file didnt.Please help me Here is my index.js file

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);


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

server.listen(3000, function(){
  console.log('listening on *:3000');
});
like image 840
Blessan Kurien Avatar asked Dec 24 '14 16:12

Blessan Kurien


People also ask

Can you run jQuery locally?

You can add jQuery using CDN or through downloading it on local machine. For Local Installation, download jQuery library on your local machine and include it in your HTML code.

Can I use jQuery in node JS?

js: We can use jQuery in Node. js using the jquery module. Note: Use the 'jquery' module not the 'jQuery' module as the latter is deprecated.

Can I put jQuery in a JS file?

Your JavaScript file ( scripts. js ) must be included below the jQuery library in the document or it will not work. Note: If you downloaded a local copy of jQuery, save it in your js/ folder and link to it at js/jquery. min.

How do I run a JavaScript file in node JS?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


2 Answers

Finally I found the answer.I simply load the jquery file from localhost by this way http://localhost/assets/jquery.min.js.

like image 61
Blessan Kurien Avatar answered Oct 11 '22 18:10

Blessan Kurien


app.use('/assets', express.static('assets'))

Put your jquery.min.js file in relative "/assets/jquery.min.js" path, then access as http://localhost/assets/jquery.min.js

"localhost" could be your ip address also.

Personally, I need this because I need a completely self contained demo to run independant of an available internet connection. Murphy's law is alive and well come conference time.

like image 40
Sean Novak Avatar answered Oct 11 '22 18:10

Sean Novak