Friends, I consider myself a javascript programmer at an intermediate level, but these questions will sound amateur, forgive me.
I was doing reverse engineer in the Ghost.org blog plataform (CMS).. trying to understand how they did it, and I found that they have three instances of express()
: parentApp, blogApp and adminApp.
parentApp
is responsible for running the site, and the other two are for separate the admin
and the blog
.
I tried to go further, but the code is a total noodles. So...
I tried to do the same to understand how can i use this configuration if I ever need...
First I created a major instance called app.js
, and other 'minor' two: site.js
and admin.js
.
I've set the template engine, the folder where each would have their viewes and sent the app
instance to listen on port 3000 (as usual).
Now comes the problem:
The site or the admin is not running, and I want to find out how to make it run. I would have to create a server and listen to those instances too?
This really puzzled me because I looked through their code and simply not been able to find as they did to listen only on parentApp and still cause the site to recognize routes, middleware, etc of the other instances.
Thank u and i'm sorry for me not perfect english, i did my best.
I don't know if this is what you're after, but you can use express just like any other middleware. Someone may be able to improve on this example, but take a look. I created an out-of-the-box express app and then just added another express instance as middleware. I stripped out all but the essentials here to make it readable.
var express = require('express');
var path = require('path');
var routes = require('./routes/index');
var users = require('./routes/users');
var adminRoutes = require('./admin/routes/index');
var app = express();
var admin = express();
admin.use('/', adminRoutes);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
admin.set('views', path.join(__dirname, 'admin/views/'));
admin.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/', admin);
module.exports = app;
In admin/routes/index.js I created a route to "/admin".
router.get('/admin', function(req, res, next) {
res.render('index', { title: 'Express Admin' });
});