Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery in HTML with node.js as backend

I'm new to jQuery and node.js and this question has been bugging me for a couple of hours, I've searched stackoverflow and googled but couldn't find a solution.

I'm testing this "hello world" example in jQuery and I tried running it with node.js, but it didn't work.

Here is my code for node.js server:

var express = require('express');
var fs = require("fs");
var app = express.createServer(express.logger());
var path = require('path');

var indexText = fs.readFileSync("test.html");

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


app.get('/', function(request, response) {
  response.send(indexText.toString());
});

var port = process.env.PORT || 8080;
app.listen(port, function() {
  console.log("Listening on " + port);
});

So as you can see, I tried use express.static() to tell the node.js where jQuery lib is located. Here is the html for test.html:

<html>
<head>
<title>jQuery Hello World</title>

<script type="text/javascript" src="/jquery/jquery-1.2.6.min.js"></script>

</head>

<body>

<script type="text/javascript">

$(document).ready(function(){
 $("#msgid").html("This is Hello World by JQuery");
});

</script>

This is Hello World by HTML

<div id="msgid">
</div>

</body>
</html>

It should print:

This is Hello World by HTML This is Hello World by JQuery

but it only prints hello from HTML

The question might be stupid but I'm new to this so I require your help. Thank you.

like image 995
Drag0 Avatar asked Jan 12 '23 21:01

Drag0


1 Answers

Ok, I found a solution to what I was asking. Here is a git repository with code that helped me:

https://github.com/jmhdez/Nodejs-Sample

and here is the tutorial with the code (it is on Spanish, but let the Google Chrome translate the entire page for you, it is ok)

http://blog.koalite.com/2011/11/tutorial-node-js-express-jquery-i-creando-la-aplicacion/

Thanks everyone once again for your help

like image 157
Drag0 Avatar answered Jan 16 '23 22:01

Drag0