The configure module

Since, we are keeping our server.js file very lean, there is still a fair amount of logic that is required in configuring our server. For this, we will defer to a custom module that we'll create called configure. To get started, create a configure.js file in the server folder. We have already installed the custom dependencies when we were installing Express in the first place.

Now that the module is installed and ready to be used, let's start writing the configure.js file. First, like any of our modules, we will declare our dependencies:

const path = require('path'), 
    routes = require('./routes'), 
    exphbs = require('express-handlebars'),), 
    express = require('express'), 
    bodyParser = require('body-parser'), 
    cookieParser = require('cookie-parser'), 
    morgan = require('morgan'), 
    methodOverride = require('method-override'), 
    errorHandler = require('errorhandler'); 
 
module.exports = (app)=>{ 
   app.use(morgan('dev')); 
   app.use(bodyParser.urlencoded({'extended':true})); 
   app.use(bodyparser.json()); 
   app.use(methodOverride()); 
   app.use(cookieParser('some-secret-value-here')); 
   routes(app);//moving the routes to routes folder. 
    
   app.use('/public/', express.static(path.join(__dirname, 
'../public'))); if ('development' === app.get('env')) { app.use(errorHandler()); } return app; };

In the preceding code, we declared const for each of the modules that we will be using in our custom configure module. Then, we defined the actual module that will be exported by this code file, more specifically a function that accepts our app object as a parameter, as well as returns that same object (after we make some configuration modifications to it).

You should see that we require Connect, which is actually installed by default with Express.js as one of its core dependencies. Connect is a popular third-party middleware framework that we will learn more about later in this chapter.

Let's take a look at each of the Connect middleware we have used in the preceding code:

  • morgan: This is the module responsible for logging. This is very helpful for debugging your Node server.
  • bodyParser: This helps to facilitate the packing of any form fields that are submitted via a HTML form submission from a browser. Form fields that are submitted via a POST request will be made available via the req.body property.
  • methodOverride: For older browsers which don't support REST HTTP verbs properly, such as UPDATE and PUT, the methodOverride middleware allows this to be faked using a special hidden input field.
  • cookieParser: This allows cookies to be sent and received.
  • errorHandler: This handles any error that occur throughout the entire middleware process. Typically, you would write your own custom errorHandler that might render a default 404 HTML page, log the error to a data store, and so on.
  • handlebars: This is the templating engine we are going to use with the Views. We will explain more about integrating it in the upcoming sections.

The routes(app) line is a special component of Express that says you are actually using a router with your server, and you can respond to requests such as GET, POST, PUT, and UPDATE. Since you are using the Express router as one of the last middleware, we will also define the actual routes in the next section.

Finally, the express.static() middleware is used to render static content files to the browser from a predefined static resource directory. This is important so that the server can serve up static files, such as .js, .css, images, and regular.html, as well as any other files you might need to serve up. The static middleware will serve up any static files from the public directory, like the following code:

http://localhost:3300/public/js/somescript.js
http://localhost:3300/public/img/main_logo.jpg

It's important that your static middleware is defined after app.router(), so that static assets aren't inadvertently taking priority over a matching route that you may have defined.

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

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