I’m looking for a PHP based CMS, easy to scale and flexible enough to be the starting point for the next generation of e-commerce website for the hosting company I work for.

Why PHP ? I started by adding scalability on top and I felt nodejs was the answer. However things are not pretty generous on CMS side. There are few names but they look at the early stage of development or suffering from the lack of features: KeystoneJS, Pencilblue, Apostrophe, Ghost.

Note: Reaction Commerce – is a really interesting project as they are the only one building something for the eCommerce industry. They are using Meteor, Node.js (note: interesting combination), MongoDB and CoffeScript and it is launched as a Docker container.

I’m going to collect the strengths and weaknesses of few PHP options available with the mention I’m going to write a separate post for Reaction Commerce.

We’ll discuss about:

Expression Engine

Craft

ProcessWire

ExpressionEngine is built by EllisLab, a company that also created CodeIgniter, a popular PHP framework for building robust web applications. ExpressionEngine 2.x is built on top of CodeIgniter.

Craft is built by Pixel and Tonic, a company who, interestingly, got started creating third-party add-ons for ExpressionEngine. Their add-ons – Playa and Matrix – are well-built, renown plugins within the ExpressionEngine community.

Data modelling

A model is simply a type of content your site stores. You might have a “blog post”, “product”, or “staff member” model. ExpressionEngine calls these model types a channel while Craft calls them a section.

JavaScript is a dynamic, object-oriented, and functional scripting language. One of the features that make it win over Java Applets in the browser scripting war decades ago, it was its lightness and non-blocking event loop.

Bocking means that when one line of code is executing the rest of it is locked waiting to finish. On the other hand, non-blocking gives to each line of code a shot and then through callbacks it can come back when an event happens. Programming languages that are blocking (Java, Ruby, Python, PHP, …) overcomes concurrency using multiple threads of execution while JavaScript handles it using non-blocking event loop in a single thread.

Some companies like Paypal moved from Java backend to NodeJS and reported a increased performance, lower average response times, and development speed gains. Similarly happens to Groupon that came from Java/Rails monoliths.

1. NPM – The Node Package Manager

When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. The idea of NPM modules is a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

Some of the most popular NPM modules today are:

express – Express.js, a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.

connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.

socket.io and sockjs – Server-side component of the two most common websockets components out there today.

Jade – One of the popular templating engines, inspired by HAML, a default in Express.js.

mongo and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.

The best way to manage locally installed npm packages is to create a package.json file.

A package.json file allows you to:

documentat what packages your project depends on

it allows you to specify the versions of a package that your project can use using semantic versioning rules

makes your build reproducible which means that its way easier to share with other developers.

To specify the packages your project depends on, you need to list the packages you’d like to use in your package.json file. There are 2 types of packages you can list:

"dependencies": these packages are required by your application in production

"devDependencies": these packages are only needed for development and testing

JavaScript

1

2

3

4

5

6

7

8

9

10

{

"name":"my_package",

"version":"1.0.0",

"dependencies":{

"my_dep":"^1.0.0"

},

"devDependencies":{

"my_test_framework":"^3.1.0"

}

}

The above package.json specify that the app uses any version of the package my_dep that matches major version 1 in production, and requires any version of the package my_test_framework that matches major version 3, but only for development.

This is a tool to manage node processes during development. With nodemon you can start a node process and it keeps it running for. It utilizes fsevents to hook into filesystem changes and it restarts the node process on each file change.

You can install it using npm using the following command. I like to install it globally so I can use it for all projects, but you can remove the -g to install it locally instead.

MS DOS

1

npminstall -gnodemon

Now instead of using node server.js to run your application, you can use nodemon server.js. It will watch for any changes in your application and automatically restart your server for you.

3. Node inspector

Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools. The really cool thing is that it works almost exactly as the Chrome Developer Tools.

MS DOS

1

npminstall -gnode-inspector

You should be a flavour of Chrome browser (Chrome, Chromium, etc) installed.

Once it is installed, you can run it using the following command. This will start the debugger and open your browser.

MS DOS

1

node-debugserver.js

Can you combine nodemon with node inspector ? You would start your server with nodemon --debug server.js and then you’ll need to run node-inspector in a separate terminal window unless you push nodemon to the background.

The platform and core framework were designed around an event-driven, non-blocking I/O model and constructing a trivial server is as simple as the following script:

JavaScript

1

2

3

4

5

6

7

varhttp=require('http');

http.createServer(function(req,res){

res.writeHead(200,{'Content-Type':'application/json'});

res.end('{ "message": "Hello World" }');

}).listen(3000,'127.0.0.1');

console.log('Server listening at http://127.0.0.1:3000/');

The server can be launched by executing the script via Node:

JavaScript

1

2

$node server.js

Server listening at http://127.0.0.1:3000/

Contributing to Node’s phenomenal growth was an excellent package management system built on lessons learned from other communities. Creating, publishing, and installing a Node module or collection of related modules as a package is simple and fast.

Packages contain their own local copy of installed modules, easing deployment since there is no requirement to install to any common file system locations (and resolve potential version conflicts).

All that is required for a package is a package.json file that contains a bit of metadata about a package and its dependencies. A minimal package.json file only requires a name and version.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

{

"name":"helloworldapp",

"version":"0.0.0",

"private":true,

"scripts":{

"start":"node ./bin/www"

},

"dependencies":{

"body-parser":"~1.13.2",

"cookie-parser":"~1.3.5",

"debug":"~2.2.0",

"express":"~4.13.1",

"jade":"~1.11.0",

"morgan":"~1.6.1",

"serve-favicon":"~2.3.0"

}

}

Nodejs benefits:

Lightweight HTTP server processes — the Node platform is based on Google’s well-regarded open source, high performance V8 engine, which compiles JavaScript to native machine code on supported systems; this machine code undergoes dynamic optimization during runtime. The V8 engine is highly tuned for fast startup time, small initial memory footprint, and strong peak performance.

Highly scalable — the Node platform was designed from the onset for end-to-end asynchronous I/O for high scalability.

Lightweight for developers — there is minimal ceremony involved in creating and consuming packages, which encourages a high degree of modularization and lightweight, narrowly-focused packages independently versioned and published.

Where Node.js Can Be Used ?

Server-side web applications / REST APIs

Pros:

If your application doesn’t have any CPU intensive computation, you can build it in Javascript top-to-bottom, even down to the database level if you use JSON storage Object DB like MongoDB.

Crawlers receive a fully-rendered HTML response, which is far more SEO-friendly than, say, a Single Page Application or a websockets app run on top of Node.js

Cons:

Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach. Alternatively, you could try scaling out the computation.

Using Node.js with a relational database is still quite a pain. Do yourself a favour and pick up any other environment like Rails, Django, or ASP.Net MVC if you’re trying to perform relational operations.

Where Node.js Shouldn’t Be Used?

Server-side web application with a relational database behind Relational DB tools for Node.js are still in their early stages; they’re rather immature and not as pleasant to work with. Still, if you’re really inclined to remain JS all-the-way (and ready to pull out some of your hair), keep an eye on Sequelize and Node ORM2.

Heavy server-side computation/processing – When it comes to heavy computation, Node.js is not the best platform around. No, you definitely don’t want to build a Fibonacci computation server in Node.js. In general, any CPU intensive operation annuls all the throughput benefits Node offers with its event-driven, non-blocking I/O model because any incoming requests will be blocked while the thread is occupied with your number-crunching. Node.js is single-threaded and uses only a single CPU core. When it comes to adding concurrency on a multi-core server, there is some work being done by the Node core team in the form of a cluster module [ref: http://nodejs.org/api/cluster.html]. You can also run several Node.js server instances pretty easily behind a reverse proxy via nginx. With clustering, you should still offload all heavy computation to background processes written in a more appropriate environment for that, and having them communicate via a message queue server like RabbitMQ.

Conclusion

Node.js was never created to solve the compute scaling problem. It was created to solve the I/O scaling problem, which it does really well.

This pattern is the same concept as the module pattern in that it focuses on public & private methods. The only difference is that the revealing module pattern was engineered as a way to ensure that all methods and variables are kept private until they are explicitly exposed; usually through an object literal returned by the closure from which it’s defined.

Advantages

Cleaner approach for developers

Supports private data

Less clutter in the global namespace

Localization of functions and variables through closures

The syntax of our scripts are even more consistent

Explicitly defined public methods and variables which lead to increased readability

Going to js-module-pattern, the pattern is used to mimic classes in conventional software engineering and focuses on public and private access to methods & variables. The aim is to improve the reduction of globally scoped variables, thus decreasing the chances of collision with other code throughout an application.

Advantages

Cleaner approach for developers

Supports private data

Less clutter in the global namespace

Localization of functions and variables through closures

Disadvantages

Private methods are unaccessible.

Private methods and functions lose extendability since they are unaccessible.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

(function(window,undefined){

// normally variables & functions start with a lowercase letter but with modules, that is not the case.

// The general tradition is to start them with a capital letter instead.

functionMyModule(){

// `this` refers to the instance of `MyModule` when created

this.myMethod=functionmyMethod(){

alert('my method');

};

// note that we still use a function declaration even when using a function expression.

// for more information on why, check out: http://kangax.github.io/nfe/

this.myOtherMethod=functionmyOtherMethod(){

alert('my other method');

};

}

// expose access to the constructor

window.MyModule=MyModule;

})(window);

// example usage

varmyModule=newMyModule();

myModule.myMethod();// alerts "my method"

myModule.myOtherMethod();// alerts "my other method"

Another example of the module pattern that exposes the module a little differently and makes use of a shared private cache. This method encourages more of an object creation approach where we can optimize performance by being efficient with shared storage.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

varMyModule=(function(window,undefined){

// this object is used to store private variables and methods across multiple instantiations

By default in Windows Server 2008 when you are using the Web Management Service (WMSVC) and Web Deploy (also known as MSDeploy) it will use Basic authentication to perform your deployments. If you want to enable Windows Authentication you will need to set a registry key so that the Web Management Service also supports using NTLM. To do this, update the registry on the server by adding a DWORD key named “WindowsAuthenticationEnabled” under HKEY_LOCAL_MACHINE\Software\Microsoft\WebManagement\Server, and set it to 1. If the Web Management Service is already started, the setting will take effect after the service is restarted.

Katana is a flexible set of components for building and hosting OWIN-based web applications.OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.

If you want to have a nice welcome screen instead of “Hello world !” just add below line of code and ignore the rest of the code from Configuration method.

C#

1

app.UseWelcomePage();

Under the hood

Owin describes one main component which is the following interface:

C#

1

Func<IDictionary<string,object>,Task>

This is a function that accepts a simple dictionary of objects, keyed by a string identifier. The function itself returns a task. The object in the dictionary in this instance will vary depending on what the key is referring to.

More often, you will see it referenced like this:

C#

1

usingAppFunc=Func<IDictionary<string,object>,Task>

An actual implementation might look like this:

C#

1

2

3

4

5

6

publicTask Invoke(IDictionary<string,object>environment)

{

varsomeObject=environment[“some.key”]asSomeObject;

// etc…

}

This is essentially how the “environment” or information about the HTTP context is passed around. Looking at the environment argument of this method, you could interrogate it as follows:

C#

1

2

varhttpRequestPath=environment[“owin.RequestPath”]asstring;

Console.WriteLine(“Your Path is:[{0}]”,httpRequestPath);

if a HttpRequest was being made to ‘http://localhost:8080/Content/Main.css” then the output would be:

Your Path is [/Content/Main.css]

In addition, while not part of the spec, the IAppBuilder interface is also core to the functioning of an Owin module (or ‘middleware’ in Owin speak):

C#

1

2

3

4

5

6

7

8

publicinterfaceIAppBuilder

{

IDictionary<string,object>Properties{get;}

objectBuild(Type returnType);

IAppBuilder New();

IAppBuilder Use(objectmiddleware,paramsobject[]args);

}

The IAppBuilder interface acts as the ‘glue’ or host to bring any registered Owin compatible libraries/modules together. With support of this simple mechanism, you can now write isolated components that deal with specific parts of functionality related to Http requests. You can then chain them together to build capabilities of my Http server. You can literally chain together Owin components to form a pipeline of only the necessary features you want. Basically you can build or use a custom host, then you can insert whatever custom modules into the Http request processing pipeline. Owin provides the specification for writing those modules that make it easy to insert into the chain.

Sample Katana component

Owin components are also known as middleware.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

usingSystem;

usingSystem.IO;

usingSystem.Collections.Generic;

usingSystem.Threading.Tasks;

usingOwin;

usingMicrosoft.Owin.Hosting;

namespaceKatanaDemo

{

usingAppFunc=Func<IDictionary<string,object>,Task>;

classProgram

{

staticvoidMain(string[]args)

{

stringuri="http://localhost:8080";

using(WebApp.Start<Startup>(uri))

{

Console.WriteLine("Started !");

Console.ReadKey();

Console.WriteLine("Stopping !");

}

}

}

publicclassStartup

{

publicvoidConfiguration(IAppBuilder app)

{

// how to use your customer component

app.Use<HelloWorldComponent>();

}

}

publicclassHelloWorldComponent

{

AppFunc _next;

publicHelloWorldComponent(AppFunc next)

{

_next=next;

}

publicTask Invoke(IDictionary<string,object>environment)

{

varresponse=environment["owin.ResponseBody"]asStream;

using(StreamWriter writter=newStreamWriter(response))

{

returnwritter.WriteAsync("Hello !!");

}

}

}

}

Wrap component in extension method and invoke it like this which resembles the Welcome page invocation.