I would like to seek help on my REST API. I have finished constructed my basic REST API for my Raspberry PI and able to work now, thanks to you guys. Now I would like to display mySQL database records in a table with loop so it can always display new records but it failed and prompt an error which I have screenshot it down and uploaded.
This is my led_rest_api.js:
var express = require('express');
var bodyParser = require('body-parser');
var gpio = require('pi-gpio');
var app = express();
var dateFormat = require('dateformat');
var mysql = require('mysql');
var http = require('http');
var now = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
var conn = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'sensor'
});
dateFormat(now);
app.use(bodyParser.json());
app.set('port', process.env.PORT || 3030);
var display = conn.query('SELECT * FROM sensorInfo');
var table = ('<tr><th>Sensor ID</th><th>Sensor</th><th>On Log Time</th><th>Off Log Time</th></tr>');
foreach (display as row) {
table. =
"<tr><td>". $row['sensorId'] . "</td>".
"<td>". $row['sensors'] . "</td>".
"<td>". $row['onLogTime'] . "</td>".
"<td>". $row['offLogTime'] ."</td></tr>";
});
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE html>");
response.write("<html>");
response.write("<head>");
response.write("<title><H1 align="center">Sensor REST API</H></title>");
response.write("</head>");
response.write("<body>");
response.write("<table border=1>table</table>");
response.write("</body>");
response.write("</html>");
response.end();
});
app.put('/:pin/1', function(req, res)
{
var pin = req.params.pin;
gpio.open(pin, 'output', function(err){
if (pin === 7){
//Set pin 7 high (1)
gpio.write(7, 1, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(1, "Red LED", current_timestamp(), NULL)', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(7);
});
return gpio;
} if (pin === 11){
//Set pin 11 high (1)
gpio.write(11, 1, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(2, "Blue LED", current_timestamp(), NULL)', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(11);
});
return gpio;
} if (pin === 16){
//Set pin 16 high (1)
gpio.write(16, 1, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(3, "Green LED", current_timestamp(), NULL)', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(16);
});
return gpio;
}
}
});
app.put('/:pin/0', function(req, res)
{
var pin = req.params.pin;
gpio.open(pin, 'output', function(err){
if (pin === 7){
//Set pin 7 low (0)
gpio.write(7, 0, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(1, "Red LED", NULL, current_timestamp())', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(7);
});
return gpio;
} if (pin === 11){
//Set pin 11 low (0)
gpio.write(11, 0, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(2, "Blue LED", NULL, current_timestamp())', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(11);
});
return gpio;
} if (pin === 16){
//Set pin 16 low (0)
gpio.write(16, 0, function()
{
conn.query('INSERT INTO sensor(sensorId, sensors, onLogTime, offLogTime) VALUES(3, "Green LED", NULL, current_timestamp())', function(err){
if (err) throw err;
else res.send('success');
});
res.send(now);
gpio.close(16);
});
return gpio;
}
}
});
server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
This is the error screenshot: