Skip to content

02. Server basics: Express, Postgres, Socket.io

Omri Bernstein edited this page May 31, 2016 · 1 revision

Express application

The code generated for the server application is fairly lean. You will find the meat of it exists to implement the Sequelize User model or to handle different authentication strategies. Otherwise, it comprises a few things.

The Express application begins its definition in the file /server/app/index.js. It uses the following third-party middleware:

If the request URL is /favicon.ico, serve up the favicon.

Parses the cookie string included in requests and places an object on req.cookies.

Parses any request body attached to a POST or PUT request and places it on req.body.

Creates and reads session cookies for user identification between requests. Places information on req.session. It uses connect-mongo to persist session information.

Used for authenticating and controlling authorization in routing. Also used are the passport-twitter, passport-facebook, passport-google-oauth authentication strategies.

Postgres and Sequelize

In the program's database initialization (server/db), a connection is established to a postgres database running on port 5432. The database URI comes from the environment configuration.

Generated also is a simple User model that has an email field and other fields for basic authentication (password/salt, google_id, facebook_id, etc). This model is registered in (server/db/models/user.js) and used throughout the server application by referencing the registered model (db.model('user')). This is the recommended way of working with Sequelize models, as opposed to requireing the file itself by location.

seed.js, located in the generated project root, can be used as a foundation for building a seed file. A seed file is helpful for development to determine the baseline data for your application to use during development. This is preferable to creating new users, new products, etc., every time you want to develop in a new environment.

Socket.io

A very simple socket.io integration is established in server/io/index.js. This gets attached to our http server created in server/start.js. In later pages, we will quickly go over how the browser application connects to the websocket.