首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从html页面执行Nodejs脚本?

从html页面执行Nodejs脚本?
EN

Stack Overflow用户
提问于 2012-03-12 23:45:57
回答 1查看 75.8K关注 0票数 20

我目前正在使用Express.js创建我的网站。我的主服务器脚本叫做index.coffee。我还创建了一个名为request.js的脚本,该脚本发出GET请求并显示响应

代码语言:javascript
复制
  console.log(list);

从控制台运行脚本时没有问题:node request.js

我的问题是:如何让页面上的"Get this list“按钮通过在同一页面上显示列表来响应点击(即在服务器上执行request.js并显示结果)?

app.js

代码语言:javascript
复制
/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set ('view engine', 'coffee');
app.register('.coffee', require('coffeekup').adapters.express);

  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.get('/', function(req, res) {
  res.render('index',{ layout: false });
});



app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

index.coffee

代码语言:javascript
复制
doctype 5
html ->

  head ->
    body

P ->“嘿”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-13 03:12:51

我使用普通的JS,而不是咖啡脚本,所以这里是Fosco评论的一个例子(称之为server.js):

代码语言:javascript
复制
var express = require('express'),
    list = require('./request.js').Request; // see  template

var app = express.createServer();

app.use(express.static(__dirname + '/public')); // exposes index.html, per below

app.get('/request', function(req, res){
    // run your request.js script
    // when index.html makes the ajax call to www.yoursite.com/request, this runs
    // you can also require your request.js as a module (above) and call on that:
    res.send(list.getList()); // try res.json() if getList() returns an object or array
});

app.listen(80);

编写index.html文件,并将其保存在节点应用程序目录的/public子文件夹中(上面通过express.static公开了该目录):

代码语言:javascript
复制
<html>
    <body>
    <div id="button">Get this List</div>
    <div id="response"></div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#button').click(function() {
            // run an AJAX get request to the route you setup above...
            // respect the cross-domain policy by using the same domain
            // you used to access your index.html file!
            $.get('http://www.yoursite.com/request', function(list) {
                $('#response').html(list); // show the list
            });
        });
    });
    </script>
    </body
</html>

如果您将request.js作为一个模块包含在内,它可能如下所示:

代码语言:javascript
复制
var RequestClass = function() {
    // run code here, or...
}; 

// ...add a method, which we do in this example:
RequestClass.prototype.getList = function() {
    return "My List";
};

// now expose with module.exports:
exports.Request = RequestClass;

在您的服务器上运行node server.js。然后转到www.yoursite.com/index.html

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9670222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档