Hardware Reference
In-Depth Information
var
server
=
http
.
createServer
(
servePage
);
b
.
pinMode
(
gpio
,
b
.
INPUT
,
7
,
'pulldown'
);
server
.
listen
(
port
);
console
.
log
(
"Listening on "
+
port
);
function
servePage
(
req
,
res
) {
var
path
=
url
.
parse
(
req
.
url
).
pathname
;
console
.
log
(
"path: "
+
path
);
if
(
path
===
'/gpio'
) {
var
data
=
b
.
digitalRead
(
gpio
);
res
.
write
(
htmlStart
+
data
+
htmlEnd
,
'utf8'
);
res
.
end
();
}
else
{
fs
.
readFile
(
__dirname
+
path
,
function
(
err
,
data
) {
if
(
err
) {
return
send404
(
res
);
}
res
.
write
(
data
,
'utf8'
);
res
.
end
();
});
}
}
function
send404
(
res
) {
res
.
writeHead
(
404
);
res
.
write
(
'404 - page not found'
);
res
.
end
();
}
This the the HTML to send to the server before the data value.
This is the HTML for after the data.
Normally, we'd send a file, but if
/gpio
is requested, we'll build our own page
and include the value of the GPIO pin.