HTML中的jQuery,node.js作为后端 [英] jQuery in HTML with node.js as backend

查看:80
本文介绍了HTML中的jQuery,node.js作为后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery和node.js的新手,这个问题已经困扰了我几个小时,我搜索了stackoverflow和google搜索,但找不到解决方案。



我在jQuery中测试了这个hello world的例子,我试着用node.js来运行它,但它没有用。



这里是我的node.js服务器代码:

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

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

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

如你所见,我尝试使用express.static()来告诉node.js在哪里jQuery lib位于。
这里是test.html的html:

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

< script type =text / javascriptsrc =/ jquery / jquery-1.2.6.min.js>< / script>

< / head>

< body>

< script type =text / javascript>
$ b $(文档).ready(函数(){
$(#msgid)。html(This is Hello World by JQuery);
});

< / script>

这是HTML世界的Hello World

< div id =msgid>
< / div>

< / body>
< / html>

它应该打印:

这是Hello World by HTML
这是JQuery的Hello World



但它仅打印HTML中的hello



这个问题可能是愚蠢的,但我是新手,所以我需要你的帮助。谢谢。

解决方案

好的,我找到了一个解决方案。
这是一个代码帮助我的git仓库:

  https://github.com/jmhdez/Nodejs - 示例

以下是带有代码的教程(使用西班牙语,但让Google Chrome翻译整个页面,这是可以的)

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

再次感谢大家寻求帮助


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.

解决方案

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

这篇关于HTML中的jQuery,node.js作为后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆