bio photo

Email

Intro

I received some fantasitc advice from Tom Igoe who lent me his book Making Things Talk and nudged me to explore Node frameworks that make different ports talk to eachother. John Farell (resident ITP) told me about Express, a minimal, flexible Node.js web application which I could use to separate the server and client side portion of my code.

Trials & readings

I got straight into modifying example code from these sources to help me figure out what I need to do,

  1. Tigoe’s ExpressIntro
  2. Pg. 386, Making Things Talk (Project 50: Phoning the Thermostat)
  3. John Farell’s Decapitated Restaurants
  4. Did the tutorial, Node.js Express Framework Tutorial
  5. Did a Twilio Tutorial for Node and Express
  6. Built a Hello World app to get started with Express
  7. Securing Node.js with Express Middleware

I was having a hard time visualizing the program jump around between functions. How it was structured and how different portions of it interacted between themselves and the browser through protocols was confusing. Terms like REST, require, request, respond etc were getting the better of me.

To get through this I did some side readings, two of the more helpful ones:

  1. How I explained REST to my wife, explains Difference between REST and HTTP]
  2. Documentation of Express

Summary

Some of the code I knew was relevant to me,

//INDEX.JS

app.use(express.static(path.join(__dirname, '/views')));
app.use(express.static(path.join(__dirname, '/public')));

app.use('/', routes);

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/views/index.html')

function indexResponse(request, response) {
  // response.send expects one long string. It also
  // sends an HTTP header (200, OK), and closes the connection:
  var textToSend = "Hello! this server responds to the following routes: <br>";
  textToSend += "/name will tell you the server's name <br>";
  textToSend += "/date will tell you today's date <br>";
  textToSend += "/data/?parameter=value will tell you what you sent. <br>";
  response.send(textToSend);
}

//ROUTE.JS

function serverStart() {
  var port = server.address().port;
  console.log('Server listening on port '+ port);
}

// this is the handler for the root of the site:
app.get('/', function (request, response) {
	var content = 'Hello. Would you like to know the name or the age?';
	content += '\n';		// add a newline at the end of the content
	response.send(content);	// send it back to the client
	response.end();			// close the connection
});

});

I realized structuring the code so that Node would know where to look for when a path gets called is important and there’s alot more to it than simple callback functions that I’m building.

A separate file needs to be configured for my Node server (HTTP or Express based) that can receive requests from the URL (which will be POST requests from input coming in via a browser page which has text input boxes). The incoming text will have to be routed to different functions.