# 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](#builtin-middleware). There are no extra steps need to use the server in your Blueprint application. You just need to configure it to meet you needs.

{% hint style="info" %}
You configure the server using the configuration file named `app/config/server.js`, or a corresponding configuration file for the node environment.
{% endhint %}

## 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`](https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener).

{% code title="app/configs/server.js" %}

```javascript
module.exports = {
  protocols: {
    http: {
      port: 8080      // default is 80
    }
  }
}
```

{% endcode %}

### 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`](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener).

{% code title="app/configs/server.js" %}

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

{% endcode %}

## 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](https://github.com/expressjs/morgan) is http request logging middleware for [Express](http://expressjs.com/).

#### Configuration

Use `middleware.morgan` property to configure [morgan](https://github.com/expressjs/morgan). If you do not provide a configuration for [morgan](https://github.com/expressjs/morgan), then a default one is provided. See the [morgan documentation for available options](https://github.com/expressjs/morgan#options).

{% code title="app/configs/server.js" %}

```javascript
module.exports = {
  middleware: {
    morgan: {
      // morgan options go here
    }
  }
```

{% endcode %}

### Body Parser

[bodyParser](https://github.com/expressjs/body-parser) is Node.js parsing middleware for [Express](http://expressjs.com/). It is responsible for parsing the body of a HTTP request and making the content available on `req.body`. [bodyParser](https://github.com/expressjs/body-parser) support different kinds of parsers, such as [JSON](https://github.com/expressjs/body-parser#bodyparserjsonoptions) and [url encoded](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions).&#x20;

#### Configuration

Use `middleware.bodyParser` to define the configuration for [bodyParser](https://github.com/expressjs/body-parser). Each parser you want to support has a named configuration under `middleware.bodyParser`. [JSON](https://github.com/expressjs/body-parser#bodyparserjsonoptions) and [url encoded](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions) body parsers are always enabled. You, however, can change the configuration of [JSON](https://github.com/expressjs/body-parser#bodyparserjsonoptions) and [url encoded](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions) body parsers, and include others supported by the [bodyParser](https://github.com/expressjs/body-parser) middleware.

{% code title="app/config/server.js" %}

```javascript
module.exports = {
  middleware: {
    bodyParser: {
      json: {
        // add bodyParser.json options here
      },
      
      urlencoded: {
        // add bodyParser.urlencoded options here
      }
    }
  }
}
```

{% endcode %}

### Express Validator

[express-validator](https://github.com/express-validator/express-validator) is middleware for validating the input of an HTTP request. It is does this by adapting [validator](https://github.com/chriso/validator.js) into an [Express](http://expressjs.com/) middleware. [express-validator](https://github.com/express-validator/express-validator) is used by [actions](https://blueprint.onehilltech.com/routers-and-controllers/controllers#actions) to [validate and sanitize input](https://blueprint.onehilltech.com/routers-and-controllers/controllers#validating-and-sanitizing-input).

### Configuration

There is no configuration support for [express-validator](https://github.com/express-validator/express-validator).

### Cookie Parser (optional)

The [cookie-parser](https://github.com/expressjs/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](https://github.com/expressjs/cookie-parser) middleware. See the [cookie-parser](https://github.com/expressjs/cookie-parser) documentation for the [available configuration options](https://github.com/expressjs/cookie-parser#cookieparsersecret-options).

{% code title="app/configs/server.js" %}

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

{% endcode %}

### Express Session (optional)

[express-session](https://github.com/expressjs/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](https://github.com/expressjs/session) middleware. See the [express-session](https://github.com/expressjs/session) documentation for the [available configuration options](https://github.com/expressjs/session#options).

{% code title="app/configs/server.js" %}

```javascript
module.exports = {
  middleware: {
    session: {
      // Add configuration options here
    }
  }
};
```

{% endcode %}

### Passport (optional)

[Passport](http://www.passportjs.org/) is authentication middleware for [Node.js](https://nodejs.org/).&#x20;

#### Configuration

Use the `middleware.passport` data property to configure [Passport](http://www.passportjs.org/). If `middleware.passport.session` is defined in the server configuration file, then the server will [configure session support with the Passport middleware](http://www.passportjs.org/docs/configure/).&#x20;

{% code title="app/configs/server.js" %}

```javascript
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 () {
        
        }
      }
    }
  }
}
```

{% endcode %}

### 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](https://expressjs.com/en/guide/writing-middleware.html), or an array of [Express middleware functions](https://expressjs.com/en/guide/writing-middleware.html).

{% code title="app/configs/server.js" %}

```javascript
module.exports = {
  middleware: {
    custom: [ 
      // add middleware functions here
    ]
  }
}
```

{% endcode %}

## 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.

{% code title="app/configs/server.js" %}

```javascript
module.exports = {
  statics: [
    '../public_html',
    '/var/files'
  ]
}
```

{% endcode %}
