3

i am accessing the script.js and libs.js in index.html like this:

<body style='margin:0px' >
<canvas id='your_canvas'
        style='position: absolute; background-color: black;'></canvas>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="/libs.js"></script>
<script type="text/javascript" src="/script.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    jQuery(function($){

        main(); //this function is inside of script.js

    });
</script>

but the .js files cannot be accessed and showing error:

1) GET script.js 404 (Not Found) localhost/:12.
2) GET libs.js 404 (Not Found) localhost/:11.
3) Uncaught ReferenceError: main is not defined (index):17.

where am i going wrong and how can i include these two JavaScript files using node.js?

my app.js is :

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

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

app.use(express.static(__dirname + '/public/script.js'));
app.use(express.static(__dirname + '/public/libs.js'));

 io.sockets.on('connection', function(socket) {
    socket.on('send coordinates',function(data){
    io.sockets.emit('coordinates', data);
    });

});

and then i have my script.js and libs.js files.

3
  • are you using express? did you configure static resource? Commented Aug 6, 2014 at 9:20
  • actually i'm totally new to these concepts and no i haven't used static resource .. please enlighten me abt it.. @MohitArora Commented Aug 6, 2014 at 9:33
  • neelsg already post the answer about this and public is folder name where js files exists. Commented Aug 6, 2014 at 9:38

2 Answers 2

2

You need to actually serve these files if you want them to be accessible. You can't just include them in your project directory. If you are using Express, you can designate a static file directory like this:

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

You can also use node-static or even create a handler for each file...

Using express, your code should look something like this (Not tested):

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

app.get('/', function(req, res){
    res.sendfile(__dirname + '/public/index.html'); // I would move this file to public
});

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

io.sockets.on('connection', function(socket) {
    socket.on('send coordinates',function(data){
        io.sockets.emit('coordinates', data);
    });
});

app.server.listen(8000);
Sign up to request clarification or add additional context in comments.

8 Comments

thanx a lot...i'll try your suggestion and will update you if it works..:)
and y u have used '/public' here..?
'/public' is just a suggestion. I strongly recommend you create a sub-directory like '/public' or '/static' to keep all your static file in. If you share your project directory statically, that would be an epic security fail (A user could get your source code by typing something like http://example.com/app.js into their browser and follow the logic to your database connection details).
i've created a folder called chat and these files are inside it.i'll craete a folder inside it called public ad save my javascript files in it and then access it the way u said..i just hope i got whatever u said ,right..:) again thnxxx
i tried it ur way but the problem still persists...do i have to make changes in the html code that i have mentioned in the starting of my ques...????
|
1

Are you trying to call:

<script>
jQuery(function($){

    main(); //this function is inside of script.js

});
</script>

If you are then you need to save your code as a file named script.js in the root directory. Adding javascript/jQuery between tags in your html does not need to be called and will run automatically.

I'd advise laying your script out in the following way too, i don't recognize the way you have laid it out.

<script>

var main = function() {
    \\Your javascript/jQuery
}

$(document).ready(main);

</script>

1 Comment

thanx i think you caught my mistake..i'll change the code..:) can you just post one up for my post...i just dont want a negative.. this ques is imp for me..i will b grateful..thnx again

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.