How to send/receive data from client to server?

Asked

Viewed 3,564 times

2

How to send/receive client data to the server, making requests as a search in the database dynamically?

  • Know the concept of Webservice and JSON? This will help you a lot with any application that needs to access data in a BD.

  • Just to clarify, what you need is an example of how to make a request?

  • Yes exactly.

2 answers

2

Keep in mind that everything on the computer is programmatic, you have to create your pattern or research patterns because the community has already made many patterns as you also wanted to make everything dynamic but the reality is different, when knowledge is little we learn rambling,so use your pattern or the studied pattern in both front-end and back-end, it’s dynamically tricky, and it’s hard for you to do anything, but if you want to send data through front-end and receive in back-end with pure code, follow code below.

I hope I helped with the code below.

FRONT-END USE VUE.JS

<template>
  <div>
    <input type="text" v-model="dado"><button v-on:click="enviar">click</button>
  </div>
</template>


<script>
  from axios import 'axios'

  export default {
    name: 'dadosenviados',
    data: function () {
      return {
        dado: ''
      }
    },
    methods: {
      enviar: function () {
        var this_ = this
        axios({
          url: "seu_ip_do_Back_end/rota", method: "POST", { data: this_.dado }
       })
      }
    }
  }
</script>

BACK-END use pure NODE.js

const http = require('http');

const server = http.createServer(function (request, response) {

  // quiser ver o url
  console.log(request.url);

  // quiser ver o method
  console.log(request.method);

  // pegando o dado.
  // assim você faz o download do dado.

  var body = '';
  request.on('data', function (data) {
    body += data;
  })

  // quando terminar o download
  request.on('end', function () {

    // formato que vio é uma string.
    console.log(body);
    console.log(typeof(body));

    // caso queira o obj
    console.log(JSON.parse(body));
    console.log(typeof(JSON.parse(body)));
  })

})

server.listen('127.0.0.1', '8181', function () {
  console.log('Server open in http://127.0.0.1:8181/');
});

If you want a database, download Mysql and save it to your project.

commando:

$ npm install mysql

now create a connection

const mysql_con = require('mysql');

const sql = mysql_con.createConnection({
  host: '127.0.0.1',
  port: '3306',
  user: 'root',
  password: 'root',
  database: 'teste'
})

I advise you to read the documentation.

1


If you’re using Express, you can receive data via app.get() or app.post()

  • app get.()

Data passed in GET (url for example: google.com?foo=bar&animal=gato) will be available via req.query. An example would be:

app.get('/pasta', function(req, res){
    var foo = req.query.foo;
    // etc
});
  • app post.()

The data passed in the POST are available via req.body. That is if you have a <form action="/pasta" ... and an input: <input type="text" name="animal" /> this data can be used so:

app.post('/pasta', function(req, res){
    var animal= req.body.animal;
    // etc
});
  • Okay, how else do I upload from the client to the server that? ex: I have a form in the file 'index.htm' and I want to send the data to the express app.js, as it would be in the html page?

  • @Rafaelvergopolan as I put in the answer must indicate the path (path) in the form property (<form action="/pasta" ...) Then Express will retrieve the form data using the property name="" of each input. You only have to have a button or input with type="submit" for the form to be sent.

  • understood, this page that contains the form has to be generated by express or it can be any?

  • @Rafaelvergopolan if I understood correctly your poergunta can be any. The important thing is that app.post('/endereco', function(req, res){ has the same address as the form action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.