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:
- Create an empty Node.js project
- Install any dependencies, e.g. request
- Create the server and have it accessible from http://localhost:4000/
- Give it two routes: http://localhost:4000/set?somekey=somevalue and http://localhost:4000/get?key=somekey
- Store the key and value in local memory on the set
- 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);