teja
teja

Reputation: 43

Including of external js while using node.js

I have code like the following for chat app using the socket.io

<htmL>
<head>
<title>Chat with socket.io and node.js</title>
<style>
#chat{
height:500px;
}
</style>
</head>
<body>
<div id="chat"></div>
<form id="send-message">
<input size="35" id="message"></input>
<input type="submit"></input>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="socket.io/socket.io.js"></script>
<script>

jQuery(function($){
var socket=io.connect();
var $messageForm=$('#send-message');
var $messageBox=$('#message');
var $chat=$('#chat');

$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message',$messageBox.val());
$messageBox.val('');
$messageBox.focus();
});

socket.on('new message',function(data){
$chat.append(data+"<br/>");
});

});
</script>
</body>
</html>

When i am running this it was working fine.

But when ever I change the jquery file with local sotred one it was not getting that file even it present there. Please solve me.

Code after modification

<script src="/jquery.min.js"></script>
<script src="socket.io/socket.io.js"></script>

How to include external js file here.

Upvotes: 0

Views: 716

Answers (1)

Jamie Barker
Jamie Barker

Reputation: 8256

Server Side jQuery

Use NPM to install jQuery on the server side: https://www.npmjs.com/package/jquery

Command Line: npm install jquery

Code: var $ = require('jquery');

Simples. *chirp*

Client Side jQuery

Place your file in a folder named "public" and load the file from there

<script src="/public/jquery.min.js"></script>

Then on server side (I'm assuming you're using express?), do:

//For Reference Only...
var express = require('express')
var app = express();

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

Then any other static files you want to load will have to be placed within the public folder.

Upvotes: 1

Related Questions