Node.js, Express, Jade & Template in Package - node.js

Is it possible to store templates for an express application in a separate package?
In my usecase I'd like to have a shared package containing global templages to give all apps the same look and feel even so they run as an independent entity on another port or even another server. Local content templates could live within the app, so all I'm looking for is a way to share that kind of code between multiple apps.
Going a step further I was thinking about skinning packages which can overwrite the default templates. Once installed in the "template package" it could change the look and feel of all applications using the core templates.
Is there a way of doing that without having to drop the comfort of express?
cu
Roman

This is possible using express. You can basically mount a whole app object to a specific route (with all routes and middleware).
var express = require('express');
var coreApp = express();
var blogApp = express();
var wikiApp = express();
// init blogApp and wikiApp with middleware and routes
coreApp.use('/blog', blogApp);
coreApp.use('/wiki', wikiApp);
Now you can mount your templates into this modular apps and then mount them into your core app.
Here's a screen cast from the express creator himself, called modular web applications.

Related

Serving static files in Express (express.static vs app.static)

Through serving static files in Express .. I saw below code:
const express = require('express');
const app = express();
// Initialize the main project folder
app.use(express.static('website'));
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Note: I tried to replace express with app and it said app.static is not a function. I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
app.static() has nothing to do with Express.
Wam is a completely different framework (that may be Express-like in some ways, but it's not Express and not identical to Express). Here's a description on the NPM wam.js page:
Wam is a small koa and next.js inspired middleware framework for node.
If you want to program with Express, then use the Express documentation, not the Wam documentation and it will guide you to use app.use(somePath, express.static()). You can see in the Express doc for the app object, there is no mention of app.static(). That is apparently something that wasm.js invented for it's own framework.
Why we didn't use app.static() instead of express.static() as we already assigned express() to the app constant, and what is the difference between them?
Because Express doesn't have app.static(). It has express.static().
I also saw some NPM packages that use app.static() like wamjs for example, which is weird.
I wouldn't call it weird. wamjs is a different package with a different API. It is not Express so there should be no expectation that Express behaves like wamjs or that wamjs behaves like Express. They are different frameworks.

What does an express app instance give you over an express router instance?

I've come across an interesting situation where we're using express app composition:
const rootApp = express();
const moduleOne = express();
const moduleTwo = express();
rootApp.use(moduleOne);
rootApp.use(moduleTwo);
rootApp.listen(3000);
Each of these module apps have their own routers, middleware, etc. and I was wondering what are some advantages and disadvantages of having app composition versus router composition at a higher level.
It generally allows for better isolation and use of middleware. For example I tend to roll with multiple Express Applications (composed together) when building CRUD based apps which use Single Page Web Apps (React, Ember, Angular, etc). This allows me to attach different headers to each incredibly easier.
It also makes it very easy to move each application into it's own repo, this could be useful if you later on wish to run each application as it's own service. You could then use something like nginx to route to two apps. For example:
myapp.com/api/auth -> authentication express app
myapp.com/api -> general API
myapp.com/ -> light weight express app which serves the front end.
This makes it much easier to scale your application later on, and allows more fine grained control.
In the short term, the control over various middleware is the thing I find most useful about composing express apps vs composing express routers. It means you can do "app.use()" and tends to read a lot better to other developers coming to your project.
An app is a router object with a few more properties and methods related to the overall state of your app. Properties on the app object like app.locals or methods like app.listen() apply to your whole app, not just to a set of routes. Other than that though, you can think of an app like a subclass of a router with all the capabilities of a router with added app properties.
If you look in the source code for the app object, you will see that it has a router in its instance data (not sure why they didn't actually subclass it):
this._router = new Router({...});
And, then further examination of the code shows that app.use(), app.get(), etc... all flow through that above router.
So, your app objects works just perfectly fine if all you need is one router. But, there are reasons to use more than one router. For example, if you have a series of sub-routes (say everything that starts with /api/xxx) that you want to do some authentication on, but lots of other routes don't require that auth, then you can create a router for /api, define middleware on only that router that checks your auth and then define the sub-routes on that router and you get a nice encapsulated definition of sub-routes all with their own middleware.

Converting Node.js command line app to web app

So this is more of an open ended question: I've started working with node and I've been creating command line applications for practice. The majority of these apps take command line arguments and make http requests to an API and serve up the results based on the arguments passed. The thing is, I would like these programs to have useful front-end interfaces so that the results are not just display via the command line terminal. Is there an easy way to accomplish this? Is this what Express is useful for?
perhaps more fully, that's what express is for and that's what routes do for you - so that your browser can be directed to a default (e.g. index.html) page or a specific page or service. If you're rendering basic html pages, stored in an /HTML folder, to the user, then you might have the following kind of code in your app:
var express = require('express');
var app = express();
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/HTML'));
followed by a series of app.get('path/from/browser') and/or app.post('path/from/broswer') statements which tell your nodejs server what to do when various get and post commands are sent to the app.
as your app gets more complex, you may want to consider the router service as a way to structure your application code and associated services.
you also need to start an http server, so the browser can actually talk to the server. You would do that in a very simple way by executing the following code:
var cfenv = require('cfenv');
var appEnv = cfenv.getAppEnv();
app.set('port', appEnv.port);
var server = app.listen(app.get('port'), function() {console.log('Listening on port %d', server.address().port);});
In this simple example, your app is now using 3 new services: express, ejs, and cfenv. You would use the standard npm install process to get this into your local app so that you can use them. From your application root folder, you would execute npm install --save express, repeating for each of the three new services.

How to mount a Sails.js app as express middleware

I would like to use Sails within a larger express based app. Most node.js MVC frameworks I have worked with you can mount as express middleware. Is this possible with Sails?
I want to do something like:
var express = require('express'),
app = express();
var mySailsApp = require('./mysailsapp');
app.use(mySailsApp);
While there are some active efforts to develop systems that would let Sails be more modularized, there's no way to use a Sails app as Express middleware. Sails works on top of Express, but not the other way around. However, you can use custom Express middleware with Sails--see this answer for an example.

Node.js Express: require/use one app within another

I'm facing the following situation. In order to further modulize my software development, I've written a few standard modules stand alone. Think for instance of an login module based upon Express and Passport, allowing users to login with all kinds of social services. The module also contains UI for user management, login, registration, profile, etc.
Now, the thing I'm trying to do is to just drop the Auth app folder (containing the express app, all it's routes, views, models, settings and dependecies) into another Express app (for instance, a CMS) and then load it with something like require('./lib/auth/app.js'). I know this is possible, take a look at Kue.
How would I go about doing this? And how do I manage namespacing problems? I could of cours append /auth/ to each route, but I can imagine the settings (app.use()'s) and public folder would conflict with the 'parent' app.js' settings and public folder.
Thanks in advance,
Fabian
Think I found my answer. So, I found this question, and this one. Guess my terminology was off.
I solved my problem by doing a few things. First of all, I changed all routes and url's to be "namespaced" (not really, but this does the job). All routes now have /auth/ in front of them. I did the same to all links, so that's all working.
Next, I removed the server part from my app.js. So, in stead of doing:
require('http').createServer(app).listen(app.get('port'));
I just do:
module.exports = app;
And I add some custom methods to the app object:
app.isLoggedIn = auth.isLoggedIn;
app.notLoggedIn = auth.notLoggedIn;
Then, in my root app, I just do the following to load the auth app in. Routing, public files, and all other stuff happens magically. pretty cool.
var auth = require('./vendor/auth/app');
var app = express();
app.configure(function() {
app.use(auth); // use our auth app
// do a lot of root-app related stuff...
});
Things still to do:
My auth app uses some session stuff. According to the second link, if I understand correctly, the app.use(session...) stuff in my auth app gets overridden by app.use. Also, I want to define an EJS helper to generate my urls (something like site_url('/facebook/callback') which then points to /auth/facebook/callback). Finally, I need to include settings from my root app. I'm thinking of wrapping my entire app.js (in auth) in a function, to which I pass a config object. Like this:
module.exports = function(config) {
var app = express();
app.set('config', config);
// various app settings, routes, etc
// return app so it's available in my root.
return app;
}
I hope this helps, if you need a bit more information I'll see if I can post some code to a gist. just let me know!

Resources