HTML中的jQuery,node.js作为后端

Dra*_*ag0 3 html jquery node.js express

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


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

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

所以你可以看到,我尝试使用express.static()告诉node.js jQuery lib所在的位置.这是test.html的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>
Run Code Online (Sandbox Code Playgroud)

它应该打印:

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

但它只打印来自HTML的hello

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

Dra*_*ag0 7

好的,我找到了解决问题的方法.这是一个git存储库,其代码帮助我:

https://github.com/jmhdez/Nodejs-Sample
Run Code Online (Sandbox Code Playgroud)

这是教程与代码(它是西班牙语,但让谷歌浏览器翻译整个页面,没关系)

http://blog.koalite.com/2011/11/tutorial-node-js-express-jquery-i-creando-la-aplicacion/
Run Code Online (Sandbox Code Playgroud)

再次感谢大家的帮助