Introduction to Node.js & Express

I used Node.js and Express to make a local server with some very basic routing features.

the name of the server is ‘lilserv’

The repository for this project and other connected devices projects can be found here. This post is contained to the ‘lilserv’ folder.

Discussion of the give-and-take, request-and-receive nature of HTTP communication made me start thinking more-critically about where data goes when we submit it, and where it can be detected. In this small server project, I hope to make an interactive in-class-exercise based on a fun economics riddle.

The Riddle

When this server is running, anyone who visits it on the right port will be sent a page with the following prompt:

Suppose everyone in ITP picks a number between 0 and 100, inclusive (i.e. 0 and 100 are both possible choices, as is any other number between).

They pick knowing that winner is the individual (or individuals) who selects the number closest to 2/3 of the average of all the numbers chosen.

What number would you choose?

After a user types a number into a field on the page and clicks submit, they are routed to a new page that links to an explanation of the game’s the rationale.

These pages are served due to a express.Router()  instance in the router.js file and some standard GET request handler:

lilrouter.get('/', function(req, res) {
 res.sendFile(path.join(__dirname + '/index.html'));

lilrouter.get('/explaination.html', function(req, res) {
 console.log('somebody submitted' + req.url);
 res.sendFile(path.join(__dirname + '/explaination.html'));

As you can see, the GET request also contains a log of the submitted value via the request’s URL (req.url).

In class I should be able to monitor submissions, and then we can all take a look together!

I chose to use the router.js as an exercise in modularity and to experience the different ways to send and receive content. I believe it was entirely unnecessary to achieve the goals of this little riddle. I also want to experiment with POSTs and other means of getting people’s submissions.

Node/Express (aka “javascript for the server-side”) will hopefully be a useful tool to move beyond static HTML and CSS files that only show content (such as my current website).


Leave a Reply

Your email address will not be published. Required fields are marked *