<commit_before>---
layout: post
title: Week 2: Servers, Streams and Promises
---

When I got to RC, I knew I wanted to wrap my head around how the web works: I had struggled to wrap my head around clients, servers, and HTTP requests. Tom suggested that I build a simple HTTP server and play around with it. I had followed nodejs tutorials before, so this time I decided I would build everything by reading only the docs. I also wanted to get a better handle on I/O - as the internals of the file system were a mystery to me, so I decided that my little app would search for and serve text from files on my computer.

###My first version of this server used standard JavaScript callbacks:

``` javascript

var http = require('http'),
	fs = require('fs'),
	url = require('url');

var server = http.createServer(function(req, res) {
//Get the request parameter from the url
	var name = url.parse(req.url, true).query.name;
//If there is a request parameter, hunt through the files for a matching filename
	if (name) {
		fs.readdir(process.cwd(), searchForFile);
	} else {
		var searchError = new Error('To search for a file, add a ?name=... to your url')
		writeError(searchError);
	}
	function searchForFile(err, files) {
		var found = false;
		if (!err) {
			files.forEach(matchFile);
			if (!found) {
				var noFile = new Error('Sorry, there is no such file!\n' +<commit_msg>Edited post to make markdown work<commit_after>---
layout: post
title: Week 2 - Servers, Streams and Promises
---

When I got to RC, I knew I wanted to wrap my head around how the web works: I had struggled to wrap my head around clients, servers, and HTTP requests. Tom suggested that I build a simple HTTP server and play around with it. I had followed nodejs tutorials before, so this time I decided I would build everything by reading only the docs. I also wanted to get a better handle on I/O - as the internals of the file system were a mystery to me, so I decided that my little app would search for and serve text from files on my computer.

###My first version of this server used standard JavaScript callbacks:

``` javascript

var http = require('http'),
	fs = require('fs'),
	url = require('url');

var server = http.createServer(function(req, res) {
//Get the request parameter from the url
	var name = url.parse(req.url, true).query.name;
//If there is a request parameter, hunt through the files for a matching filename
	if (name) {
		fs.readdir(process.cwd(), searchForFile);
	} else {
		var searchError = new Error('To search for a file, add a ?name=... to your url')
		writeError(searchError);
	}
	function searchForFile(err, files) {
		var found = false;
		if (!err) {
			files.forEach(matchFile);
			if (!found) {
				var noFile = new Error('Sorry, there is no such file!\n' +
{'fraction_non_alphanumeric': 0.09022013713460844, 'fraction_numerical': 0.0007217610970768675, 'mean_word_length': 3.863157894736842, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256849', 'n_tokens_mistral': 810, 'n_tokens_neox': 773, 'n_words': 409}