 if server.get('env') is 'test'
   server.set 'port', PORT_TEST
 
-# Start the server.
-port = server.get 'port'
-http.createServer(server).listen port, ->
-  env = server.settings.env
-  console.log "The qiprofile server is listening on port #{port} in #{env} mode."
+# Start MongoDB, if necessary...
+spawn 'mongod', MONGODB_PORT, ->
+  # ...then the REST app...
+  spawn 'qirest', EVE_PORT, ->
+    #...then the Express server.
+    port = server.get 'port'
+    http.createServer(server).listen port, ->
+      env = server.settings.env
+      console.log "The qiprofile server is listening on port #{port} in #{env} mode."
 
 module.exports = server

# The API route.
server.use '/api', api()

# Serve static and partial files directly.
server.get '/static/*', (req, res) ->
  path = root + req.path.replace('/static', '')
  res.sendfile path
server.get '/partials/*', (req, res) ->
  res.sendfile "#{root}/#{req.path}.html"

# Serve index for all qiprofile routes.
server.get '/quip*', (req, res) ->
  res.sendfile "#{root}/index.html"


express = require 'express'
http = require 'http'
path = require 'path'
net = require 'net'
forever = require 'forever-monitor'
authenticate = require 'authenticate'
spawn = require './spawn'

server = express()

PORT = 3000
PORT_TEST = PORT + 1

MONGODB_PORT = 27017

EVE_PORT = 5000

# The grunt build tasks place all compiled and copied files within
# the _public directory.
