Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const mysql = require('mysql');
- var bodyParser = require('body-parser');
- var urlencodedParser = bodyParser.urlencoded({ extended: true });
- //Crear objeto de conexion:
- var con = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'tic3',
- socketPath: ''
- });
- // Establecer conexion:
- con.connect(function(err) {
- if (err) {
- console.error('Error de conexion: ' + err.stack);
- return;
- }
- console.log('Conectado con el ID: ' + con.threadId);
- });
- const app = express();
- app.post('', urlencodedParser, function (req, res) {
- console.log("Temperatura: " + req.body['temp']);
- console.log("Humedad: " + req.body['hum']);
- if (!req.body) return res.sendStatus(400);
- var datos = req.body;
- console.log("datos:", datos);
- res.send('Los datos han sido recibidos por el servidor.');
- var sql = "UPDATE dispositivos SET temp='" + req.body['temp'] + "', hum='" + req.body['hum'] + "' WHERE id='123'";
- con.query(sql, function (err, result) {
- if (err) throw err;
- console.log("Numero de filas afectadas: " + result.affectedRows);
- });
- });
- app.listen(port='3000', () => {
- console.log("Servidor corriendo en puerto", port);
- });
- // Update
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement