Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const mysql = require('mysql');
  3. var bodyParser = require('body-parser');
  4. var urlencodedParser = bodyParser.urlencoded({ extended: true });
  5.  
  6. //Crear objeto de conexion:
  7. var con = mysql.createConnection({
  8.   host: 'localhost',
  9.   user: 'root',
  10.   password: '',
  11.   database: 'tic3',
  12.   socketPath: ''
  13. });
  14.  
  15. // Establecer conexion:
  16. con.connect(function(err) {
  17.   if (err) {
  18.     console.error('Error de conexion: ' + err.stack);
  19.     return;
  20.   }
  21.   console.log('Conectado con el ID: ' + con.threadId);
  22. });
  23.  
  24. const app = express();
  25.  
  26. app.post('', urlencodedParser, function (req, res) {
  27.   console.log("Temperatura: " + req.body['temp']);
  28.   console.log("Humedad: " + req.body['hum']);
  29.   if (!req.body) return res.sendStatus(400);
  30.  
  31.   var datos = req.body;
  32.   console.log("datos:", datos);
  33.   res.send('Los datos han sido recibidos por el servidor.');
  34.  
  35.   var sql = "UPDATE dispositivos SET temp='" + req.body['temp'] + "', hum='" + req.body['hum'] + "' WHERE id='123'";
  36.  
  37.                              con.query(sql, function (err, result) {
  38.                                if (err) throw err;
  39.                                console.log("Numero de filas afectadas: " + result.affectedRows);
  40.                              });
  41. });
  42.  
  43. app.listen(port='3000', () => {
  44.   console.log("Servidor corriendo en puerto", port);
  45. });
  46.  
  47. // Update
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement