DEV Community

Ibrahim Ragab
Ibrahim Ragab

Posted on

Write Node server with ES6 Classes

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 and so on.

I wrote a simple server using ES6 classes and it looks like this:

When I log the response it seems typical to the regular server, check the repository below you can clone and run it.
I need answers about what is the advantages and the difference.

GitHub logo Yuribenjamin / node-server-class

Such a node server is written with ECMAScript 2015 (ES6)

node-server-class

Such a Express Node server is written with ECMAScript 2015 (ES6) JavaScript classes.




Top comments (3)

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.