function worked automatically (constructor) when file required in node.js - node.js

I couldnt write function worked automatically (constructor) when file required in node.js..
this is so...file.js
module.exports = {
index: function () {
//code here
}
};
app.js
var file=require("./file");
res.send(file.index());
what I want...
module.exports = {
main :__constructor()
{
this.name="blabla";
},
index: function () {
//code here
this.name // will be used this place name variable
}
};

Use .prototype() to do this. Hope it helps!
const App = function(name) {
this.name = name
}
App.prototype.main = function() {
return this.name
}
b = new App('myName')
console.log(b.main())
//You can do module.exports = App and invoke it in another file

okey..but
module.exports = {
app :function (name)
{
this.name="blabla";
},
app.prototype.index: function () {
}
};
this does not work!!!
/var/www/public/nodeapi/app/v1/blog/index.js:19
app.prototype.index: function () {
const App = function(name) {
this.name = name
}
App.prototype.main = function() {
return this.name
}
b = new App('myName')
console.log(b.main())
//You can do module.exports = App and invoke it in another file

Something like this?
// file.js
class MyClass {
constructor() {
this.name = 'blabla';
}
index() {
console.log('name', this.name);
}
}
module.exports = new MyClass();

class blog {
constructor()
{
this.name="fookey";
}
index(callback)
{
var data={};
data.test=this.name;
callback(data);
}
}
module.exports=new blog();
TypeError: Cannot read property 'name' of undefined
why does not work?

Related

how to clear global var between tests in nodejs?

I have 2 tests which each one them pass but when i run them together the second test fails.
I have this global var (CURR_GUID) which i want to clear between the tests but i am not sure how to do that.
Since it is not cleared between the tests , its value is still "1111111" and not null.
Any idea?
Class W
"use strict";
var CURR_GUID = null;
class W {
constructor() {
console.log("W ctor")
}
async getDataFromDb() {
if (CURR_GUID) {
return CURR_GUID;
} else {
this.db = require("./Connection").getDb();
console.log("W getDataFromDb")
let res = null;
res = await this.db.one("SELECT guid FROM table", ["NONE"]);
return res.guid;
}
}
async saveGuid(guid) {
this.db = require("./Connection").getDb();
await this.db.none("update table set guid=$1", [guid]);
CURR_GUID = guid;
console.log("currr is" + CURR_GUID);
}
}
module.exports = W;
Class X
"use strict";
const wDao = new (require("../db/W"))();
class X {
async getGuid() {
console.log("X getGuid")
let guid = await wDao.getDataFromDb();
console.log("got guid " + guid);
await wDao.saveGuid(guid);
return guid;
}
}
module.exports = X;
Test.spec.js
const chai = require("chai");
const expect = chai.expect;
const X = require("../src/service/X");
const Connection = require("../src/db/Connection");
const sinon = require("sinon");
describe("component_test", function () {
afterEach(function () {
this.dbStub.restore();
});
it("X_component_test", async function () {
var db = {
one: async () => {
return {
guid: '1111111',
created_on: "123"
}
},
none: async () => {
}
};
this.dbStub = sinon.stub(Connection, "getDb").returns(db);
var dao = new X();
var guid = await dao.getGuid();
expect(guid).to.equal('1111111');
});
it("X_component_test_2", async function () {
var db = {
one: async () => {
return {
guid: '222222',
created_on: "123"
}
},
none: async () => {
}
};
this.dbStub = sinon.stub(Connection, "getDb").returns(db);
var dao = new X();
var guid = await dao.getGuid();
expect(guid).to.equal('222222');
});
});
While it should be possible to achieve the desired result by deleting W and X modules from require.cache in afterEach hook, it might be better to modify the source code a bit so that it can be tested without cache manipulations.
Class W
Turn CURR_GUID variable into an instance property.
"use strict";
class W {
constructor() {
console.log("W ctor");
this.CURR_GUID = null;
}
async getDataFromDb() {
if (this.CURR_GUID) {
return this.CURR_GUID;
} else {
this.db = require("./Connection").getDb();
console.log("W getDataFromDb")
let res = null;
res = await this.db.one("SELECT guid FROM table", ["NONE"]);
return res.guid;
}
}
async saveGuid(guid) {
this.db = require("./Connection").getDb();
await this.db.none("update table set guid=$1", [guid]);
this.CURR_GUID = guid;
console.log("currr is" + this.CURR_GUID);
}
}
module.exports = W;
Class X
Turn wDao from variable into an instance property
"use strict";
const WDao = require("../db/W");
class X {
constructor() {
this.wDao = new WDao();
}
async getGuid() {
console.log("X getGuid")
let guid = await this.wDao.getDataFromDb();
console.log("got guid " + guid);
await this.wDao.saveGuid(guid);
return guid;
}
}
module.exports = X;
In this way every test should work with fresh instances of W and X that do not depend on the closure state.

Builder pattern ES6 nodejs

I've been having troubles when I try to call any builder "setter" methods
This is my code, User.js:
class User {
constructor(userBuilder) {
this.email = userBuilder.email;
this.name = userBuilder.name;
this.surname = userBuilder.surname;
this.photo = userBuilder.photo;
this.tokens = userBuilder.tokens;
this.authorized = userBuilder.authorized;
}
getEmail() {
return this.email;
}
getName() {
return this.name;
}
getSurname() {
return this.surname;
}
getPhoto() {
return this.photo;
}
getTokens() {
return this.tokens;
}
isAuthorized() {
return authorized;
}
static get UserBuilder() {
class UserBuilder {
constructor() {
this.email = null;
this.name = null;
this.surname = null;
this.photo = null;
this.tokens = null;
this.authorized = null;
}
email(email) {
this.email = email;
return this;
}
name(name) {
this.name = name;
return this;
}
surname(surname) {
this.surname = surname;
return this;
}
photo(photo) {
this.photo = photo;
return this;
}
tokens(tokens) {
this.tokens = tokens;
return this;
}
authorized(authorized) {
this.authorized = authorized;
return this;
}
build() {
return new User(this);
}
}
return UserBuilder;
}
}
module.exports = User
And my UserTest.spec.js:
process.env.NODE_ENV = 'test'
//Import User class
let User = require('../../../../main/com/auth/user/User');
// Import chai
let chai = require('chai');
// Tell chai that we'll be using the "should" style assertions.
chai.should();
describe('User', () => {
let user;
var email = "a#a";
var name = "a";
var surname = "a";
var photo = "p";
var authorized = true;
beforeEach(() => {
//Create new User object before each test
user = new User.UserBuilder()
.email(email)
.name(name)
.surname(surname)
.photo(photo)
.authorized(authorized)
.build();
});
it('returns the email', () => {
user.getEmail().should.equal(email)
});
it('returns the name', () => {
user.getName().should.equal(name)
});
it('returns the surname', () => {
user.getSurname().should.equal(surname)
});
it('returns the photo', () => {
user.getPhoto().should.equal(photo)
});
it('returns the authorized', () => {
user.getAuthorized().should.equal(authorized)
});
})
I get TypeError: user.email is not a function.
I've tried with UserBuilder outside User class:
In User.js
module.exports = {User: User, UserBuilder: UserBuilder}
In UserTest.spec.js
let {User} = require('../../../../main/com/auth/user/User');
let {UserBuilder} = require('../../../../main/com/auth/user/User');
user = new UserBuilder()
.email(email)
.name(name)
.surname(surname)
.photo(photo)
.authorized(authorized)
.build();
With the same result, what i'm doing wrong?? I can call build method, but I can't call any other methods.

how to require a class in node.js

my code that works..
var express=require("express"),
app=express();
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var newbar=new bar();
newbar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
var newbar=new bar();
newbar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
but..following code does not work.. with require that class from other file..
test.js
module.exports = {
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
};
app.js file.. with require that class
var express=require("express"),
app=express();
require("./test")
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var newbar=new bar();
newbar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
newbar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
but why does not it work...please help me..above code that works but this code that does not work..
When exporting classes in node you need to define the class first, then export the class using module.exports followed by the name of the class you wish to export.
// test.js
class Bar {
constructor() {
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
module.exports = Bar
From there you can just require the file and grab the class as such.
var Bar = require('./test');
var bar = new Bar();
test.js isn't valid syntax - you shouldn't have your entire file wrapped in braces like that. module.exports is just a variable which you set; if you want to export bar, set it to bar:
class bar {
...
}
module.exports = bar;
Additionally, you need to assign the result of your require call in app.js.
var bar = require("./test");
(On a slightly pedantic note - it's more idiomatic to captialize your class names!)
hello again #DominicValenciana
test.js
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
module.exports = bar;
app.js
var express=require("express"),
app=express();
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var bar=new require('./test');
bar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
var bar=new require('./test');
bar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
as error : TypeError: bar.name is not a function

how to call methods with variable in node.js

how to call methods with variable in node.js
this is so that ;
var file=require("./file");
var method=b;
file.method();
//output
//error : TypeError: file.method is not a function
how to use it?
ok try this then
//file.js
module.exports = {
index: function () {
console.log("index called");
},
index2 :function() {
console.log("index2 called");
} ,
index3 : function () {
console.log("index3 called");
}
};
then
app.get("file/:method",function (req,res)
{
var method = req.params.name;
var file = require('./file');
file[method]();
}
app.get("file/:method",function (req,res)
{
var file("./file");
var method=req.params.name;
file.method();
this is what ı want to tell

How to implement inheritance in node.js modules?

I am in process of writing nodejs app. It is based on expressjs. I am confused on doing inheritance in nodejs modules. What i am trying to do is create a model base class, let's say my_model.js.
module.exports = function my_model(){
my_model.fromID = function(){
//do query here
}
}
Now i want to use those methods in my_model in my other model class. let's say user_model.js
How do i inherit my_model in user_model?
in base_model:
function BaseModel() { /* ... */ }
BaseModel.prototype.fromID = function () { /* ... */ };
module.exports = BaseModel;
in user_model:
var BaseModel = require('relative/or/absolute/path/to/base_model');
function UserModel() {
UserModel.super_.apply(this, arguments);
}
UserModel.super_ = BaseModel;
UserModel.prototype = Object.create(BaseModel.prototype, {
constructor: {
value: UserModel,
enumerable: false
}
});
UserModel.prototype.yourFunction = function () { /* ... */ };
module.exports = UserModel;
Instead of using Object.create() directly, you can also use util.inherits, so your user_model becomes:
var BaseModel = require('relative/or/absolute/path/to/base_model'),
util = require('util');
function UserModel() {
BaseModel.apply(this, arguments);
}
util.inherits(UserModel, BaseModel);
UserModel.prototype.yourFunction = function () { /* ... */ };
module.exports = UserModel;
With ES6 the usage of util.inherits() is discouraged in favor of ES6 class and extends
const EventEmitter = require('events');
class MyStream extends EventEmitter {
constructor() {
super();
}
write(data) {
this.emit('data', data);
}
}
const stream = new MyStream();
stream.on('data', (data) => {
console.log(`Received data: "${data}"`);
});
stream.write('With ES6');
Using utility.inherits can also help you decouple the child from the parent.
Instead of calling the parent explicitly, you can use super_ to call the parent.
var BaseModel = require('relative/or/absolute/path/to/base_model'),
util = require('util');
function UserModel() {
this.super_.apply(this, arguments);
}
util.inherits(UserModel, BaseModel);
utility.inherits source:
var inherits = function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false
}
});
};

Resources