DEV Community

Write Node server with ES6 Classes

Ibrahim Ragab on July 04, 2019

I'm learning Node.js and all back-end stuff and I see there's one way to implement a server by import the module and declare the path and the app a...
Collapse
 
fibric profile image
Denny Trebbin

In my opinion, it's not the best example to show the OOP style server code.

Server.core + performanceHooks -> Babeljs -> repl.it
repl.it/repls/GrowingBustlingScales

Server.listen + performanceHooks -> Babeljs -> repl.it
repl.it/repls/LameUpsetPdf

Expressjs + performanceHooks -> Babeljs -> repl.it
repl.it/repls/GrubbyMediumConversion

I'm not a fan of OOP, and I'm not strictly bound to pointless or functional programming, but the given an example is, at least to me, not a good example.

Also, the compiled code isn't shorter or faster than the code we've used writing for years.
The Server.listen example is as fast as Expressjs while Server.core example is up to 4 times slower.

Note: benchmarks are always wrong because so many invisible (implicit) variables have an impact on them.

Collapse
 
yuribenjamin profile image
Ibrahim Ragab

Thanks Denny I understand now

Collapse
 
alvisonhunter profile image
Alvison Hunter Arnuero | Front-End Web Developer • Edited

Why would I create a class for an existing Singleton Pattern entity, such as Express.js, just to create methods that merely call the inherited methods from the instantiated property of Express itself? . Your example assigns the app parameter to this.app property, being a direct referral of the express() Express method.

Your proposed approach seems redundant. The purpose of a class is not to create a static blueprint but rather a flexible one that allows not only the use of existing methods and properties from its parent but also the extension of its own whenever needed. In this example, what purpose does the class serve with this approach?

This code seems overrated for a simple task like instantiating a new Express server.