How can I add a JavaScript file to my node.js application?

I am accessing 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 the error is displayed:

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:

 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 script.js and libs.js files.

+3


source to share


2 answers


You need to actually serve these files if you want them to be available. You can't just include them in your project directory. If you are using Express you can specify 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 an expression, your code should look something like this (untested):

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

      

+2


source


You are trying to call:

<script>
jQuery(function($){

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

});
</script>

      

If you need to, you must save your code as a file named script.js in the root directory. Adding javascript / jQuery between tags in your html doesn't need to be called and run automatically.



I would suggest to lay out your script like this, I don't understand how you put it.

<script>

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

$(document).ready(main);

</script>

      

+1


source







All Articles