Skip to content
Alistair Madden edited this page Mar 12, 2018 · 3 revisions

app.js is the entry point to the server side program. When node app.js is run, NodeJS starts running the program as a foreground process in the current terminal. This means it is also connected to the input and output of the terminal (and hence why you can see informational messages such as 'Example app listening on port 9000!').

npm start calls node app.js indirectly - see package.json for exactly where this occurs.

app.js is responsible for a lot of things including:

  • handling get requests
  • handling post requests (currently only one - for sending an email via the webpage)
  • validating input to requests
  • authenticating email post request via reCAPTCHA (to prevent bots submitting requests)

In future, these responsibilities should probably be split over more files.

First, let's look at how HTTP requests are handled:

To handle any kind of HTTP request, we must use a web server (ok, we could write our own web server, but honestly, the security implications and time investment make this infeasible). A web server listens on an TCP socket which is made up of a IP address and a port number. The standard port number for HTTP is port 80. When you input a URL into a web browser, the web browser automatically assumes the port number based on the protocol in use (in this case HTTP). app.js uses the non-standard port number 9000 when it runs as running on port 80 requires elevated (sudo) permissions.

The web server we've chosen to use for the CompSoc website is Express. It is very popular and the documentation online is quite good (make sure you are looking at the right version however! The version of Express we are using can be found in package.json).

var app = express();

creates an object we can do useful things with for example

app.use(express.static('public'));

makes everything in the public directory accessible... Well it would, if the web server was running. We do this at the bottom of app.js by saying

app.listen(9000, function() {
  console.log('Example app listening on port 9000!');
});

Any HTTP GET requests to a file name in the public directory will return with a 200 OK response and should be shown (if requested through a web browser).

Clone this wiki locally