Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webserver Tutorial - Should report actual ip address #119

Open
dzeitman opened this issue May 12, 2016 · 1 comment
Open

Webserver Tutorial - Should report actual ip address #119

dzeitman opened this issue May 12, 2016 · 1 comment

Comments

@dzeitman
Copy link

I'd like to suggest a modification to this page in the t2-start docs:
http://tessel.github.io/t2-start/webserver.html

Getting the Tessel device's IP address is useful for debugging and enhancing connectivity.
OS library adds this functionality.

Refactored Tutorial Code: (rename to .js to run)
webserver.js.txt

Added os interface and simple loop to push the ip addresses to an array.
Added ip address to response to display the actual IP address.

Added:

var os = require('os');

// Get a list of network interfaces:
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal) {
            addresses.push(address.address);
        }
    }
}

// Log the address []
console.log(addresses);

Changed:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with "Hello from Tessel!" to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello from Tessel!\n" +  addresses.join('\n'));
});

// Listen on port 8080, IP defaults to 192.168.1.101. Also accessible through [tessel-name].local
server.listen(8080);

// Put a friendly message in the terminal showing first address:
console.log("Server running at http://" +addresses[0]+ ":8080/");
@Frijol
Copy link
Member

Frijol commented May 12, 2016

cc @HipsterBrown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants