6

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');
});
4
  • 1
    What do you need jQuery on server side for ? Maybe you want to load it on index.html ? Commented Dec 24, 2014 at 16:48
  • Yes i want to load file in client Commented Dec 24, 2014 at 16:51
  • Than just place it as you would normally do on index.html Commented Dec 24, 2014 at 17:22
  • 1
    But it didnt work .i will get the error cannot GET jquery file Commented Dec 25, 2014 at 6:43

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

Comments

1
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.

Comments

0

You might want allow Express framework to render HTML and pass in the static jQuery files. That's how I would do it.

This page explains how you can restructure your app to serve jquery files through the node routes instead of HTML code.

1 Comment

Its little bit complicated.
0
app.get('/jquery', function(res, req){
   res.sendFile(__dirname + '/jquery.js');
});

2 Comments

Could you please explain this?
Sorry for late explanation. This means response will be jquery.js if you send request to baseurl/jquery.
0

This is what worked for me. (inside app.js)

const express = require('express');//path to express module
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));

My file structure looked like this:

  server.js
  app.js
> public
    index.html
  > assets
    > js
        jquery.min

Then import jquery from index.html like normal:

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

Comments

-1

One workaround is to use the online source file link. Try loading the jquery using the following,

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

That works fine at my end.

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.