简体   繁体   English

如何从服务器端nodejs访问客户端javascript变量?

[英]How to accessing client side javascript variable from server side nodejs?

I want to access the variable "content" on to the client side from the server side, how would I do this? 我想从服务器端访问客户端的变量“ content”,我该怎么做?

extends layout

block content
div(id="ineditortxt")
    h1(name="headings") #{title}
    a(href='/signout', class='text-center new-account') Sign Out
    div(id="editor")
        |public class #{title}{
        |   public static void main(String[] args) {
        |   
        |   }
        |}
    script(src="src/ace.js", type="text/javascript")
    script(type="text/javascript").
            var editor=ace.edit("editor");
            editor.setTheme("ace/theme/monokai");
            editor.getSession().setMode("ace/mode/java");
            function getText(){
                event.preventDefault();
                var content = editor.getValue();
                return false;
            }
    form(name="Save", id = "save", onsubmit="getText()")
        input(type="submit", value="Save")

This dependes on what protocol you want to use for your application. 这取决于您要为应用程序使用哪种协议。

If you use classical http and you run an express (http) server in node, you should follow something as described in this post . 如果您使用传统的http,并且在node中运行了Express(http)服务器,则应遵循本文中描述的内容 Your client is sending a http-post request to the server, like this you can access a variable from a html form. 您的客户端正在向服务器发送http-post请求,这样您就可以从html表单访问变量。 In pure html the form looks something like this: 在纯html中,表单看起来像这样:

<form action="/save" method="post">
<input id="save" type="text" />
<input type="submit" value="Save" />
</form>

The URL you would catch on the server side using express would be http://your-page.com/save 您将使用express在服务器端捕获的URL是http://your-page.com/save

app.post('/save', function(req, res) { ...

You can also use the "modern" WebSocket protocol , where client and server can "speak a bit more naturally with each other" but which is also quite new and not always easy to integrate to the existing http-infrastructur. 您还可以使用“现代” WebSocket协议 ,客户端和服务器之间可以“更加自然地交流”,但是它又是新事物,并不总是很容易集成到现有的http-infrastructur中。 For websocket you can use the module: socket.io for node, also in combination with an express http server. 对于websocket,可以将模块: socket.io用于节点,也可以与Express HTTP服务器结合使用。

Then it would look like something like this: 然后看起来像这样:

var express = require('express'),
    app = express(),
    server = require('http').Server(app),
    // Implementation of the WebSocket protocol.
    io = require('socket.io')(server);
server.listen(3000); // port your server is listening, test with localhost:3000

io.on('connection', function (socket) {
  socket.on('save', function (content) {
    // do something with your content variable
  });
});

And on the client side (using jquery): 并在客户端(使用jquery):

var socket = io();
$('#save').click(function () {
  socket.emit('save', content);
});

If you decide to use websocket, I would recommend you to look at this easy chat implementation . 如果您决定使用websocket,建议您看一下这个简单的聊天实现

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

相关问题 从客户端JavaScript安全地将变量传递到Node.js服务器 - Securely passing variable from client side javascript to nodejs server 如何从服务器端更改客户端javascript的变量? - How to change client side javascript 's variable from server side? 隐藏变量Vs服务器变量在javascript中访问客户端 - Hidden variable Vs Server variable accessing on client side in javascript 如何将变量从客户端 NodeJS 传递到服务器端? - How do I pass a variable from client side NodeJS to server side? 如何使用来自 javascript(客户端)的变量从 node.js(服务器端)检索数据? - How to retrieve data from node.js (server side) using a variable from javascript (client side)? 将变量从服务器端控制器传递到客户端,以在客户端JavaScript中使用 - Pass variable from server-side controller to client-side to use in client-side JavaScript 如何使用JQUERY使用AJAX将Javascript变量(从客户端)发布到PHP文件(服务器端) - How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY 如何从客户端共享变量到服务器端? - How to share a variable from the client side to the server side? 用客户端访问和设置服务器端变量的方式 - The way of accessing and setting server-side variable with client-side 在服务器端Groovy页面中访问客户端jquery变量 - accessing client side jquery variable in server side groovy page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM