Server.js – where it all begins

Whenever you write a Node app, you always need to start somewhere. The typical convention while building servers with Node is that you have a single server.js file located within the root of your project. This file will boot up the server and start the whole process. In our case, this is the file that will create the HTTP server and listen for all HTTP events, which is ultimately the point of our entire application.

We are going to keep our server.js pretty lean so that its contents are very self-explanatory. Any major logic that is going to be executed within this file will actually be defered to external modules hosted within other files.

Before we can do anything within server.js, we need to require a few modules that we're going to work with, specifically Express:

 var express = require('express'),
    // config = require('./server/configure'),
    app = express();

In the preceding code, we are assigning the Express module to the variable express. The config module is actually going to be our own module that we will write shortly, but for now, since it doesn't exist, we will leave that line commented out. Finally, we will declare a variable called app that is actually what the Express framework returns when it is executed. This app object powers our entire app, which is how it was so cleverly named.

Note

Throughout this chapter and the remainder of the book, I may include commented out code in the samples (code that starts with //). This is so that following along will be easier when we use the commented lines as reference points, or we can enable those features by simply uncommenting the code.

Next up, we will set a few simple settings via the app object using the app.set() function. These settings are really just a way for us to define some app-level constants that we can use throughout the rest of our code as handy shortcuts:

app.set('port', process.env.PORT || 3300);
app.set('views', __dirname + '/views'),
// app = config(app);

The first two lines of the preceding code use built-in constants in Node. The process.env.PORT constant is an environment setting that is set on the actual machine for the default port value to the server. If no port value is set on the machine, we will hardcode a default value of 3300 to use in its place. After that, we set the location of our views (HTML pages) to __dirname + '/views' or, using another Node constant, the /views folder from within the current working directory. The third line of code is referencing the config module, which you haven't written yet, so that line is commented out.

Last but not least, we will create an HTTP server using our app object and tell it to listen for connections:

var server = app.listen(app.get('port'), function() {
    console.log('Server up: http://localhost:' + app.get('port'));
});

Here we will create a variable to hold an instance of the server. The server is created by executing the listen function on our app that tells it which port to listen to (in the default case, 3300) and passing in a simple anonymous callback function that will execute once the server is up and listening by executing a simple console.log() message. That's it! Again, make sure to save this file with the name server.js within the root of the project. You're ready to run your server and see if it works.

Booting up server.js

Let's take your server for a spin and see how you're doing so far:

$ node server.js
Server up: http://localhost:3300

Perfect! At this point, your server doesn't actually do anything; it just listens on port 3300 but doesn't actually respond. Try this by pointing a browser to http://localhost:3300. You should receive a pretty basic message that just reads Cannot GET /. This is because you haven't configured any routes or any actual logic in your server to say how to handle certain requests, namely a GET request to the default route of /. Before you set up your routes, however, let's first take care of that config module and finish up the configuration of the server.

Note

A note about environment variables: You can set any number of environment variables right from the command line before you run your server by executing something like the following command:

$ PORT=5500 node server.js
Server up: http://localhost:5500

You can also set environment variables in your environment settings permanently. This can be done typically by editing your .profile file or equivalent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.83.126