简体   繁体   English

如何将变量从客户端传递到Express API

[英]How to pass variables from client to Express api

I've completely new to node.js and express. 我对node.js和Express完全陌生。 I've done some calculations with javascript on my client page and I want to send that generated array of numbers to my node.js server app. 我已经在客户端页面上使用javascript完成了一些计算,并且希望将生成的数字数组发送到我的node.js服务器应用程序。 When I had to send form data I just used form action to my route. 当我必须发送表单数据时,我只是将表单操作用于路由。 Now I'm totally lost and don't know where to start at. 现在我完全迷路了,不知道从哪里开始。 What code should I write in my route and how to pass js variable or array to it from my client app? 我应该在路由中编写什么代码,以及如何从客户端应用程序将js变量或数组传递给它?

to send an array of data to the Express app running on your server, the most simple way is to send it as an JSON object. 要将数据数组发送到服务器上运行的Express应用程序,最简单的方法是将其作为JSON对象发送。 A simple example using jQuery is shown below: 下面显示了一个使用jQuery的简单示例:

Client code: 客户代码:

    var your_calculated_array = []; // Set your calculated array here
    $.ajax({ 
      type: 'POST', 
      url: 'YOUR_EXPRESS_ENDPOINT', 
      data: { your_data: your_calculated_data }, 
      dataType: 'json',
      success: function (data) { 
        // Handle the response here
      }
    });

Server-side code (using body-parser middleware to parse json body): 服务器端代码(使用主体解析器中间件解析json主体):

.....

var bodyParser = require('body-parser')
app.use( bodyParser.json() );   

app.post(YOUR_EXPRESS_ENDPOINT, function(req, res) {
    var calculated_data = req.body.your_data
    // ...
})

.....

Simple Example 简单的例子

app.js app.js

var express = require('express')
var app = express()

app.set('view engine', 'pug')

app.get('/', function (req, res) {
  res.render('index')
})

app.post('/example_route', function (req, res) {
  res.send({'msg': 'Hello World!'})
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

views/index.pug views / index.pug

doctype html
html
  head
    title= 'HomePage'
    script(src='https://code.jquery.com/jquery-3.2.1.min.js')
  body
    button(onclick='$.post("http://localhost:3000/example_route", function(data) {alert(data.msg);})') Click me!

This homepage includes jquery from cdn and onclick event makes a POST request to your server. 此主页包含来自cdn的jquery,onclick事件向您的服务器发出POST请求。

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

相关问题 Express,如何将变量从路由传递到客户端的JS文件 - Express, how to pass variables from routes to JS file that is in the client side 我应该如何从 React 客户端输入搜索变量到 Express 服务器端 API 调用? - How should I input search variables from React Client Side to Express Server Side API Call? 如何使用Express从单独的文件为客户端创建api? - How to create api for client from separate file using express? Node Express将变量从客户端传递到服务器 - Node Express pass variable from client to server 从快速传递多个变量到玉 - Pass multiple variables from express to jade 将变量从Express传递给客户端JavaScript - Pass variable from Express to Client JavaScript 如何将对象传递给客户端Javascript - NodeJS&express - How to Pass an Object to client Javascript - NodeJS & express 如何在express 4. *中传递全局变量的引用 - how to pass reference of global variables in express 4.* 如何将整数变量从PHP传递到Javascript Google Chart API? - How to pass integer variables from PHP to a Javascript Google Chart API? 如何在允许清理HTML但阻止XSS的同时将数据从Node / Express服务器传递到客户端(JavaScript)? - How to pass data from a Node/Express server to the client (JavaScript) while allowing sanitized HTML but preventing XSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM