Creating the application's entry point

After installing Express and the necessary dependencies, the next step in developing the application will be to create a file which will serve as the default entry point of this application. We will be executing this file to start our web application, and it will contain the necessary code to require dependent modules and boot up the application to listen to a specified port on the development server.

We are going to name the entry point file server.js for now and will keep it pretty lean so that its content is quite self-explanatory. Any major logic that is going to be executed within this file will actually be deferred to external modules hosted within other files.

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

const express = require('express'); 
// config = require('./server/configure'); 
let app = express(); 

In the preceding code, we are assigning the express module to the express variable. 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 application, which is how it was so cleverly named.

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 when we 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 code is explained as follows:

  • 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 templates) to
    ${__dirname}'/Views, or using another Node constant, the /Views
    folder within the current working directory.
  • The third line of code is referencing the config module, which we haven't written yet, so that the line is commented out.
  • Last but not least, we will create a HTTP server using our app object and tell it to listen for connections:
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(app.get('port'), () => {
console.log(`Server up: http://localhost:${app.get('port')}`);
});

Here, we set a route in our application to respond with a Hello World message. If any user requests for the root of our application, it will respond with a Hello World message. The last section of the code is to call the listen() function on our app that tells it which port to listen to, and to pass 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.

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

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