Created
September 6, 2016 20:02
-
-
Save JordiCorbilla/5fce6fc27682e33f6b39efeb955b60f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* webapi.js | |
* | |
* @version 1.0 : August 2016 | |
* | |
* DESCRIPTION: | |
* A simple server-side application to demonstrate running a node | |
* API Application Server on a Raspberry Pi to ping other nodes | |
* Uses the Express, http and ping node packages. | |
* | |
* | |
* @throws none | |
* @see nodejs.org | |
* @see express.org | |
* | |
* @author Jordi Corbilla | |
* (C) 2016 | |
* MIT Licensed | |
*/ | |
'use strict' | |
var http = require('http'); | |
var express = require('express'); | |
var ping = require('ping'); | |
var app = express(); | |
var DEFAULT_PORT = 3000 | |
// configure Express to deliver index.html and any other static pages stored in the home directory | |
app.use(express.static(__dirname)); | |
// Express route for incoming requests with method status/xxx where xxx is the machineName | |
app.get('/status/:machinename', function (req, res) { | |
console.log('id = ' + req.params.machinename); | |
ping.sys.probe(req.params.machinename, function (isAlive) { | |
var msg = isAlive ? 'host ' + req.params.machinename + ' is alive' : 'host ' + req.params.machinename + ' is dead'; | |
console.log(msg); | |
var s = [{host: isAlive ? 'alive' : 'dead'}]; | |
res.status(200).send(s); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment