1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
Finally es6 classes have landed in Node.
Lots of people don't know it, but you can use and extend real classes in Node. js already. There's a few drawbacks, but once you learn about them, they're really not drawbacks but postive things, that will make your code faster and better.
Yes, your example would work fine.
As for exposing your classes, you can export
a class just like anything else:
class Animal {...}
module.exports = Animal;
Or the shorter:
module.exports = class Animal {
};
Once imported into another module, then you can treat it as if it were defined in that file:
var Animal = require('./Animal');
class Cat extends Animal {
...
}
Just treat the ES6 class name the same as you would have treated the constructor name in the ES5 way. They are one and the same.
The ES6 syntax is just syntactic sugar and creates exactly the same underlying prototype, constructor function and objects.
So, in your ES6 example with:
// animal.js
class Animal {
...
}
var a = new Animal();
module.exports = {Animal: Animal};
You can just treat Animal
like the constructor of your object (the same as you would have done in ES5). You can export the constructor. You can call the constructor with new Animal()
. Everything is the same for using it. Only the declaration syntax is different. There's even still an Animal.prototype
that has all your methods on it. The ES6 way really does create the same coding result, just with fancier/nicer syntax.
On the import side, this would then be used like this:
const Animal = require('./animal.js').Animal;
let a = new Animal();
This scheme exports the Animal constructor as the .Animal
property which allows you to export more than one thing from that module.
If you don't need to export more than one thing, you can do this:
// animal.js
class Animal {
...
}
module.exports = Animal;
And, then import it with:
const Animal = require('./animal.js');
let a = new Animal();
The ES6 way of require is import
. You can export
your class and import it somewhere else using import { ClassName } from 'path/to/ClassName'
syntax.
import fs from 'fs';
export default class Animal {
constructor(name){
this.name = name ;
}
print(){
console.log('Name is :'+ this.name);
}
}
import Animal from 'path/to/Animal.js';
Using Classes in Node -
Here we are requiring the ReadWrite module and calling a makeObject(), which returns the object of the ReadWrite class. Which we are using to call the methods. index.js
const ReadWrite = require('./ReadWrite').makeObject();
const express = require('express');
const app = express();
class Start {
constructor() {
const server = app.listen(8081),
host = server.address().address,
port = server.address().port
console.log("Example app listening at http://%s:%s", host, port);
console.log('Running');
}
async route(req, res, next) {
const result = await ReadWrite.readWrite();
res.send(result);
}
}
const obj1 = new Start();
app.get('/', obj1.route);
module.exports = Start;
ReadWrite.js
Here we making a makeObject method, which makes sure that a object is returned, only if a object is not available.
class ReadWrite {
constructor() {
console.log('Read Write');
this.x;
}
static makeObject() {
if (!this.x) {
this.x = new ReadWrite();
}
return this.x;
}
read(){
return "read"
}
write(){
return "write"
}
async readWrite() {
try {
const obj = ReadWrite.makeObject();
const result = await Promise.all([ obj.read(), obj.write()])
console.log(result);
check();
return result
}
catch(err) {
console.log(err);
}
}
}
module.exports = ReadWrite;
For more explanation go to https://medium.com/@nynptel/node-js-boiler-plate-code-using-singleton-classes-5b479e513f74
In class file you can either use:
module.exports = class ClassNameHere {
print() {
console.log('In print function');
}
}
or you can use this syntax
class ClassNameHere{
print(){
console.log('In print function');
}
}
module.exports = ClassNameHere;
On the other hand to use this class in any other file you need to do these steps.
First require that file using this syntax:
const anyVariableNameHere = require('filePathHere');
Then create an object
const classObject = new anyVariableNameHere();
After this you can use classObject
to access the actual class variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With