ParkDyel ParkDyel - 1 month ago 6
Javascript Question

my Node.js Application's reaction rate is very slow

First, I'm not good at speaking English.

I'm trying to write code as Node.js based on Fingersquared in Raspberry Pi.

So, I want just control my RPi'GPIO. But when I click the button,

console.log
shows very slowly or not at all (but when I click each buttton at first time very quickly react)

When I check the cpu rate, it is just normal (less than 2%).

what is my fault ? :(

app.js

var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, gpio_led = require('./routes/led');

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 52237);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/led/:num/:switch', function(req,res){
var num = req.params.num
, sw = req.params.switch;

console.log('LED ' + num + ' is ' + sw + '.');
//gpio_led(num,sw);
})


http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});


index.jade

extends layout

block content
.row
.six.columns.centered
.panel
.row
h4.text-center LED_01
.six.columns.text-center
a.button.large.right.LED1_ON ON
.six.columns
a.button.large.left.LED1_OFF OFF
.row
hr
h4.text-center LED_02
.six.columns
a.button.large.right.LED2_ON ON
.six.columns
a.button.large.left.LED2_OFF OFF
.row

script(type='text/javascript')
$('.LED1_ON').on('click', function(){
$.get('/led/1/on');
});
$('.LED1_OFF').on('click', function(){
$.get('/led/1/off');
});

Answer

Add a res.send('ok') to your handlers. The browser is waiting for a response to the first button click and never gets it. That is probably what is causing the delay.