The Server

Learn how to configure the server

Overview

The server is the part of the Blueprint application responsible for handling request. It is essentially a Wrapper Facade for an Express application. The server exists to provide functionality that is common across all Express application, such as listening on ports and installing middleware in the correct order. There are no extra steps need to use the server in your Blueprint application. You just need to configure it to meet you needs.

You configure the server using the configuration file named app/config/server.js, or a corresponding configuration file for the node environment.

Protocols

The server natively supports the http and https protocol. You configure the protocols under the protocols section of the server configuration file.

http

Here is an example configuration for http where we are setting the port number. The http hash is the options support for http.createServer.

app/configs/server.js
module.exports = {
  protocols: {
    http: {
      port: 8080      // default is 80
    }
  }
}

https

Here is an example configuration for https where we are setting the port number, public key, and private key. The https hash is the options support for https.createServer.

app/configs/server.js
module.exports = {
  protocols: {
    https: {
      port: 8443,      // default is 443
      key: blueprint.assetSync ('ssl/server.key'),
      cert: blueprint.assetSync ('ssl/serer.cert')
    }
  }
}

Built-in Middleware

All built-in middleware is configured under the middleware property. If a specific middleware configuration is not defined and it does not have a default configuration, then it is not loaded. The server supports the following Blueprint middleware, in order.

Morgan

morgan is http request logging middleware for Express.

Configuration

Use middleware.morgan property to configure morgan. If you do not provide a configuration for morgan, then a default one is provided. See the morgan documentation for available options.

app/configs/server.js
module.exports = {
  middleware: {
    morgan: {
      // morgan options go here
    }
  }

Body Parser

bodyParser is Node.js parsing middleware for Express. It is responsible for parsing the body of a HTTP request and making the content available on req.body. bodyParser support different kinds of parsers, such as JSON and url encoded.

Configuration

Use middleware.bodyParser to define the configuration for bodyParser. Each parser you want to support has a named configuration under middleware.bodyParser. JSON and url encoded body parsers are always enabled. You, however, can change the configuration of JSON and url encoded body parsers, and include others supported by the bodyParser middleware.

app/config/server.js
module.exports = {
  middleware: {
    bodyParser: {
      json: {
        // add bodyParser.json options here
      },
      
      urlencoded: {
        // add bodyParser.urlencoded options here
      }
    }
  }
}

Express Validator

express-validator is middleware for validating the input of an HTTP request. It is does this by adapting validator into an Express middleware. express-validator is used by actions to validate and sanitize input.

Configuration

There is no configuration support for express-validator.

The cookie-parser middleware in responsible for parsing HTTP request cookies and making them available via req.cookies.

Configuration

Use the middleware.cookies property to configure the cookie-parser middleware. See the cookie-parser documentation for the available configuration options.

app/configs/server.js
module.exports = {
  middleware {
    cookies: {
      secret: 'sshhh',    // secret for signing cookies
      options: { }        // [optional] an object that is passed to cookie.parse
    }  
  }
}

Express Session (optional)

express-session is very simple session middleware for Express. The session information is available on the req.session data property.

Configuration

Use the middleware.session property to configure the express-session middleware. See the express-session documentation for the available configuration options.

app/configs/server.js
module.exports = {
  middleware: {
    session: {
      // Add configuration options here
    }
  }
};

Passport (optional)

Passport is authentication middleware for Node.js.

Configuration

Use the middleware.passport data property to configure Passport. If middleware.passport.session is defined in the server configuration file, then the server will configure session support with the Passport middleware.

app/configs/server.js
module.exports = {
  middleware: {
    passport: {
      // optional Passport session configuration
      session: {
        // method for serializing a session to a response
        serializer () {
        
        },
        
        // method for deserializing a session from a request
        deserializer () {
        
        }
      }
    }
  }
}

Custom (optional)

Custom middleware is middleware functions that should be applied the entire application that are not suited for inclusion via a router, and not built-in middleware supported by the server.

Configuration

Use the middleware.custom property to configure custom middleware. The custom.middleware property takes either a single Express middleware function, or an array of Express middleware functions.

app/configs/server.js
module.exports = {
  middleware: {
    custom: [ 
      // add middleware functions here
    ]
  }
}

Static Files

Static files are assets on the server that are not dynamically generated. For example, an icon image file, a style sheet, or a javascript file could be considered a static file. The server is not considered with each individual static file, but where the static files reside.

Configuration

You use the static property in the server configuration file to define the location of the static files. The static property takes an array of paths to the static files. If the path is a relative path, then it is is relation the app/ directory. If the path is an absolute path, then the specified path is used as-is.

app/configs/server.js
module.exports = {
  statics: [
    '../public_html',
    '/var/files'
  ]
}

Last updated