Express app and a Ghost blog together

If you know anything about Ghost development surely you know Ghost is based with the Express framework over the Node.js server.

Well, maybe someday you want to add an extra superpower to your blog taking advantage of your javascript development skills over Node.js.
You can put it all together and play with all this stuff.

Let's see how.

Ghost is available as Node module in the Npm repository and the solution is as easy as install the needed modules.

You can see in the dependencies section how the two required packaged were added in the descriptive package.json file.

{
  "name": "openshift-ghost-quickstart",
  "description": "Openshift Ghost Quickstart (SQLite)",
  "repository": {
    "type": "git",
    "url": "git://github.com/openshift-quickstart/openshift-ghost-quickstart.git"
  },
  "bugs": "https://github.com/openshift-quickstart/openshift-ghost-quickstart/issues",
  "main": "index.js",
  "scripts": {
    "start": "node index"
  },
  "engines": {
    "node": "~0.10.0"
  },
  "engineStrict": true,
  "dependencies": {
    "express": "^4.13.3",
    "ghost": "^0.7.0"
  }
}

Here we decided to use an explicit version of the Express module instead the used inside the Ghost module but nothing forbids you to import any module already included in the Ghost project, or any other module, inside your code with a line like

var that = require('./node_modules/MyModule/node_modules/AnotherModule');

And here comes the magic!

In order to run only a server including the Ghost and your own module. In the script file that runs the app in our case, you should force your app to use and share the same process that starts the blog engine:

This is the index.js

var path = require('path');
var ghost = require('ghost');

var express = require('express');
var app = express();

var routes = express.Router();

routes.get('/', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  res.json({
          message: "Hello world! "
        })
});

app.use('/api', routes);


ghost({
    config: path.join(__dirname, 'config.js')
}).then(function (ghostServer) {
    // ghostServer.start();
    app.use(ghostServer.config.paths.subdir, ghostServer.rootApp);
    ghostServer.start(app);
});

That's the trick! 💡