简体   繁体   English

从页面javascript按钮运行Node.js

[英]Run Node.JS from page javascript button

What I'd like to do: User clicks a button on a webpage, it executes a node.js script that does a server-side action on the node.js page. 我想做的是:用户单击网页上的一个按钮,它执行一个node.js脚本,该脚本在node.js页面上执行服务器端操作。

Example: Every time someone clicks on a button in the page, Node.js outputs a message on the server console. 示例:每次有人单击页面中的按钮时,Node.js都会在服务器控制台上输出一条消息。

What I could do so far: I can exhibit a page with node.js + express. 到目前为止,我可以做的是:我可以展示一个带有node.js + express的页面。 I just can't make the server-side actions happen. 我只是无法使服务器端操作发生。

        <button type="button" onclick="testF()">Click</button>
        <script>
        function testF(){
            alert('Hello world!');
            console.log('clicked!!'); //Id like this to show on the node.js console
        }
        </script>

Thank you! 谢谢!

You don't need use express. 您不需要使用快递。 Node.js is really simple. Node.js非常简单。

According with the other members, you must use AJAX, so... jQuery is not necessary too. 根据其他成员的说法,您必须使用AJAX,因此... jQuery也不必要。

Look the following code that I made for you (remember only that I made a really weak code because if I write a more secure code would be possibly bigger than you expect). 请看下面为您编写的代码(请记住,我编写的代码确实很弱,因为如果编写更安全的代码,可能会比您期望的要大)。

test.html 的test.html

<button type="button" onclick="testF()">Click</button>
<script>
  function testF()
  {
    alert('Hello world!');

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("get", "/service");

    xmlhttp.onreadystatechange = function()
    {
      // DONE
      if (xmlhttp.readyState == 4)
      {
        switch(xmlhttp.status)
        {
          case 200:
            alert("OK");
            break;
          case 404:
            alert("Not Found");
            break;
          case 500:
            alert("Internal Server Error");
            break;
          default:
            alert("Unexpected Error. HTTP Status: " + xmlhttp.status);
        }
      }
    };

    xmlhttp.send();
  }
</script>

server.js (Node.js) server.js (Node.js)

var nsHttp = require("http");
var nsUrl = require("url");
var nsPath = require("path");
var nsFs = require("fs");

var srv = nsHttp.createServer(function(req, res)
{
  var pathname = nsUrl.parse(req.url).pathname;

  // check URL to send the right response
  switch(pathname)
  {
    case "/favicon.ico":
      res.end();
      break;

    case "/":
      HTTP_SendHtmlFile(res, nsPath.join(__dirname, "test.html"));
      break;

    case "/service":
      console.log("clicked!");
      HTTP_SendOK(res, "");
      break;

    default:
      HTTP_SendNotFound(res);
  }
});

// reads a file contents and sends, but if any error occur,
// sends a 500 HTTP Status Code (Internal Server Error)
function HTTP_SendHtmlFile(res, filepath)
{
  nsFs.readFile(filepath, function(err, data) {
    if (err) {
      HTTP_SendInternalServerError(res);
      return;
    }

    HTTP_SendOK(res, data);
  });
}

function HTTP_SendOK(res, body)
{
  res.writeHead(200, {"Content-type": "text/html"});
  res.end(body);
}

function HTTP_SendInternalServerError(res)
{
  res.writeHead(500, {"Content-type": "text/html"});
  res.end();
}

function HTTP_SendNotFound(res)
{
  res.writeHead(404, {"Content-type": "text/html"});
  res.end();
}

srv.listen(8080);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM