How to Make a Simple Database Server

A couple months ago, I started writing an http server using Node.js; but had to stop because, life. I've written a few of these before, as the foundation for larger Node projects using Express for routing and MongoDB for data storage; but never used local data storage. This is the first of three posts. For the first part this program, I will be creating the database server using only what's available to me with Node.js. Before I jump into the code, a quick outline sets me up for a smooth(ish) execution:

  1. Create an empty Node.js project
  2. Install any dependencies, e.g. request
  3. Create the server and have it accessible from http://localhost:4000/
  4. Give it two routes: http://localhost:4000/set?somekey=somevalue and http://localhost:4000/get?key=somekey
  5. Store the key and value in local memory on the set
  6. Retrieve the key on the get

CREATING THE EMPTY NODE PROJECT

Create a new directory and cd into it.

mkdir simple-database
cd simple-database
npm init

This creates the package.json file to store the dependencies. Then, create the main file file that will contain the server code:

touch index.js

INSTALLING DEPENDENCIES

npm install request --save

Check the package.json file:

{
  "name": "simple-server",
  "version": "1.0.0",
  "description": "simple database server",
  "main": "index.js",
  "scripts": {
  },
  "author": "your name",
  "license": "ISC",
  "dependencies": {
    "request": "^2.79.0"
  }
}

Running these dependencies adds the node_modules folder in the root directory. Installing the request package allows you to make http requests. The --save option automatically adds the package to the package.json file and saves that version to the project in the node_modules directory. 

WRITING THE SERVER CODE

The first step in creating an http server is to require the http module. http modules are included when Node is installed so no additional steps are needed to import it. Using the http module's createServer( ) method, the server is now created. The createServer( ) method takes a callback function as a parameter. Every time the server receives a new request, the callback function is executed. At this point, you can check the terminal and browser window ... each should say 'This works.' If you refresh three times, you should see 'This works' logged three times in the terminal.

The callback function takes two parameters, a request and a response. The request object contains information such as the URL, while the response object is used to return the headers and content to the user making the request.

The callback function begins by calling the res.writeHead() method which sends an HTTP status code - in this case it's 200, an indication of success - and response headers to the user making the request. If you don't specify headers, Node will send them for you. Next, the server calls the res.end() method which tells the server that the response headers have been sent and the request has been fulfilled. It can be called with no parameters, but in this case, I've included a message 'This works' as an indication that the request has been fulfilled.

The listen() method activates the server on port 4000. 

var http = require('http');

http.createServer(function(req, res){
    console.log('This works.');
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('This works.');
}).listen(4000);

Temperature Converter in Vanilla JavaScript

As far as simple projects go, this one is right up there with the nest of them. I took the idea of a temperature converter and applied some visual interest. At it's core, the project is made up of two basic functions -- one to convert the temperature in each direction and the other to change the color of each temperatures' background. I wrote this project several months ago, when I was just diving into front end using vanilla javascript. I'm sure that if I were to refactor this code today, it would look much cleaner.

If you'd like to play with the actual project, check it out here.

Giving Back by Donating My Time

Last week I organized my first RailsBridge for Womens' event at Yammer. One of the first tech events I attended when I moved to the Bay Area almost two years ago, was a RailsBridge. At that event, I made friends that I am still in contact with today. So, as the new year approached, I began thinking about ways I could get more involved in the tech community. I decided to volunteer.

I have been the RailsBridgeSF Venue Manager for a brief two months now and have attended most of the events since taking on this position. However, this was my first time pulling one of these workshops together and I was quite nervous. Why? I'm not a developer. I didn't really know what to expect and I didn't want to sound like I didn't know what I was talking about. But, it turns out, the entire experience was one of the best I've had organizing an event...ever.

So, I wanted to take the time to briefly lay out what  it really takes to organize a RailsBridge event like the one pictured below.

RailsBridge for Women | @Yammer

RailsBridge for Women | @Yammer

First, you need to find a space to hold the event, one that has at least five breakout rooms with monitors to display the curriculum and can hold at least 10 people each. But you won't have to worry about the logistics of that because you have me to handle it for you. All you have to do, is post the event to BridgeTroll and create an Issue on GitHub.

Two weeks out, open communications with the host organizer on the venue side (that relationship would already have been established by yours truly). Talk to them about catering options and ask them to help you find a good post-workshop hangout spot. Keep an eye on the RSVP list to make sure people are signing up and that you have enough teachers and TAs to maintain a nice student-teacher ratio. You'll need to provide the host organizer with final* attendance numbers (with a breakdown of dietary restrictions) the week of the event. *People always drop out at the last minute.

One week before, you can update and start practicing your opening (and closing) presentation -- it's a breeze! Do a dry-run (via auto-arrange, sooooo simple) of the student section sizes in the organizer console. Make sure you have all the day-of supplies you need, power cords, name tags, sharpies etc (we can help with that). Check (in the console) to see if anyone needs a babysitter and contact a service (again, we'll help).

So now it's Friday, the day of the InstallFest, set up a welcome table to check people in. Do a super quick intro about who you are and have the TAs raise their hands. Then let people get to installing.

On the day of the workshop, have someone help you man the welcome desk and check people in. You do the opening presentation (don't forget to thank the host and sponsors) and arrange the sections. Once everyone is in their class, you pretty much keep time for the rest of the day. Make sure people get regular breaks, let them know when lunch is and give a 30-minute warning at 4P so you can have closing remarks and retrospectives completed by 4:30P.

There you have it! Simple, right? Now head over to that happy hour.