简体   繁体   English

如何在 Meteor 中将数据从客户端发送到服务器?

[英]How to send data from client to server in Meteor?

I'm working on one app and have faced one problem.我正在开发一个应用程序,但遇到了一个问题。 I have HTML page in './public' folder.我在“./public”文件夹中有 HTML 页面。 In './public/js' I have a script that collects some data after filling the form on this page.在“./public/js”中,我有一个脚本,它在填写此页面上的表单后收集一些数据。 Then I need to send this data to a server, where some calculations with this data will be done.然后我需要将这些数据发送到服务器,在那里将使用这些数据进行一些计算。 After this server should send the data back so I can display it on HTML result page.在此服务器应该将数据发回之后,以便我可以在 HTML 结果页面上显示它。 The idea is that I need to hide this code from the client.这个想法是我需要对客户端隐藏此代码。

Any ideas how it can be done?任何想法如何做到?

EDIT: Got an idea how it can be realized.编辑:知道如何实现它。

In server/main.js I have WebApp.connectHandlers.在 server/main.js 我有 WebApp.connectHandlers。 Also I use connect-route package.我也使用连接路由包。 So I need to create post xhr in public/example.js and put values in it.所以我需要在 public/example.js 中创建 post xhr 并将值放入其中。 the url should be '/someurl' the same as in router.get('/someurl', .....), right? url 应该是 '/someurl' 和 router.get('/someurl', .....) 一样,对吧? How can it be done correctly?如何正确完成?

Here's some code from server/main.js what I have now:这是我现在拥有的 server/main.js 中的一些代码:

    WebApp.connectHandlers.use(connectRoute(function (router) {
      router.get('/example', staticFile(process.cwd() + '/../web.browser/app' + 'example.html'))

The thing is I get some values from form in example.html with .js file stored in /public.问题是我从 example.html 中的表单中获取一些值,.js 文件存储在 /public 中。 Then I create xhr post request and indicate url that should go as the first arg in router.get() in server/main.js.然后我创建 xhr post 请求并指出应该作为 server/main.js 中 router.get() 中的第一个 arg 的 url。

Fragment of /public/example.js code: /public/example.js 代码片段:

    const values = document.querySelector("input").value  //example of some data from form
    const xhr = new XHRHttpRequest()
    xhr.open('POST', '/someurl')
    xhr.send(values)

And now I need to get this request in server/main.js, but I can't use router.get('/example',.....) twice on one url.现在我需要在 server/main.js 中获取这个请求,但是我不能在一个 url 上使用 router.get('/example',.....) 两次。 I mean it won't work like this:我的意思是它不会像这样工作:

    WebApp.connectHandlers.use(connectRoute(function (router) {
      router.get('/example', staticFile(process.cwd() + '/../web.browser/app' + 'example.html'))
      router.get('/example', (req, res, next) {...});

Probably I'm not right about it but haven't found out yet how it works.可能我不正确,但还没有发现它是如何工作的。 So what can I do now?那我现在能做什么?

https://docs.meteor.com/api/methods.html https://docs.meteor.com/api/methods.html

Meteor.call("yourMethodName", (err, result) => {
  // Put your code to update HTML with result
});

Also probably a bad idea to have your html in the public folder if you want to use Meteor.call.如果您想使用 Meteor.call,将 html 放在公共文件夹中也可能是一个坏主意。

I've worked on this problem and solved it.我已经解决了这个问题并解决了它。 In server/main.js I add this code:在 server/main.js 中,我添加了以下代码:

router.post('/method/example', (req, res, next) => {
    let data = '';
    req.on("data", chunk => {
      data += chunk;
    });
    req.on('end', () => {
      const result = dataHandler(data);
      res.write(`${result}`);
      res.end();
    });
}

And in /public/example.js I just did an xhr post request with the same URL as in new line in server/mains.js.在 /public/example.js 中,我刚刚使用与 server/mains.js 中的新行中相同的 URL 进行了 xhr 发布请求。 And here's how it looks like:这是它的样子:

const xhr = new XMLHttpRequest();
xhr.open('post', '/method/example');
xhr.send(data);
xhr.addEventListener("load", () => {
    const reqResult = xhr.responseText;
}

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

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