admin管理员组

文章数量:1359230

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');
});

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');
});
Share Improve this question asked Dec 24, 2014 at 16:24 Blessan KurienBlessan Kurien 1,6759 gold badges33 silver badges64 bronze badges 4
  • 1 What do you need jQuery on server side for ? Maybe you want to load it on index.html ? – Rosmarine Popcorn Commented Dec 24, 2014 at 16:48
  • Yes i want to load file in client – Blessan Kurien Commented Dec 24, 2014 at 16:51
  • Than just place it as you would normally do on index.html – Rosmarine Popcorn Commented Dec 24, 2014 at 17:22
  • 1 But it didnt work .i will get the error cannot GET jquery file – Blessan Kurien Commented Dec 25, 2014 at 6:43
Add a ment  | 

6 Answers 6

Reset to default 1

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

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 pletely self contained demo to run independant of an available internet connection. Murphy's law is alive and well e conference time.

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.

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

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>

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

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

That works fine at my end.

本文标签: javascriptLoading local jqueryjs file in node jsStack Overflow