Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

343
Visualizações
How to pass variables from client to Express api

I've completely new to node.js and 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. 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?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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. A simple example using jQuery is shown below:

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):

.....

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

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

.....
about 4 years ago · Santiago Trujillo Relatório

0

Simple Example

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

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda