how to require a class in node.js - 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

Related

TypeError: dbConn.GetInstance is not a function

I trying to create a common database connection class using typescript for my nodejs express application that returns the MongoDB database object as follows but I always get TypeError: dbConn.GetInstance is not a function
const config = require("config");
const MongoClient = require('mongodb').MongoClient;
export class dbConn {
private db = null;
static url = config.database.uri;
static options = {
bufferMaxEntries: 0,
reconnectTries: 5000,
useNewUrlParser: true,
useUnifiedTopology: true,
};
private constructor() { }
private static instance: dbConn;
public static GetInstance() {//I also tried removing static keyword but still the error remains
if (dbConn.instance == null)
{
dbConn.instance = new dbConn();
}
return dbConn.instance;
}
public getDb() {
if (dbConn.instance.db) {
return dbConn.instance.db;
}
MongoClient.connect(dbConn.url, dbConn.options, function(err: any, db: any){
if(err) {
console.log(err);
return null;
}
dbConn.instance.db = db.db(config.database.name);
return dbConn.instance.db;
});
}
}
Updated 01-Aug-2020
I invoke the above instance from app.ts and my controllers as follows:
app.ts file
const dbConn = require('./utils/db/dbConn');
...//code removed for clarity
const app = express();
const server = http.createServer(app);
...//code removed for clarity
server.listen(port, ()=> {
dbConn.GetInstance().getDb();//I get the error here
console.log('Server running')
});
module.exports = app;
my controller file
getAll = async (pageNumber:any, pageSize:any) : Promise<PageResult<Team>> => {
return new Promise<PageResult<Team>>(async function (resolve, reject){
let result = new PageResult<Team>(pageSize, pageNumber);
var dbo = dbConn.GetInstance().getDb();//same error here too.
var query = {};
var recCount = await dbo.collection("teams").find().count();
if (recCount == 0) {
result.IsSuccessful = true;
result.ReasonForFailure = process.env.NO_RECORDS || "No record(s) to show.";
return resolve(result);
}
if (pageSize == -1) { //-1 means to return all records
dbo.collection("teams")
.find(query)
.sort({ name: 1 })
.toArray(function(err: any, resultSet: any) {
if (err) {
result.IsSuccessful = false;
result.ReasonForFailure = err.message;
return reject(result);
} else {
result.IsSuccessful = true;
result.TotalRecords = recCount;
result.PageNumber = parseInt(pageNumber);
result.PageSize = parseInt(pageSize);
result.Data = resultSet;
return resolve(result);
}
});
} else {
dbo.collection("teams")
.find(query)
.sort({ name: 1 })
.skip((parseInt(pageNumber)-1)*parseInt(pageSize))
.limit(parseInt(pageSize)).toArray(function(err: any, resultSet: any) {
if (err) {
result.IsSuccessful = false;
result.ReasonForFailure = err.message;
return reject(result);
} else {
result.IsSuccessful = true;
result.TotalRecords = recCount;
result.PageNumber = parseInt(pageNumber);
result.PageSize = parseInt(pageSize);
result.Data = resultSet;
return resolve(result);
}
});
}
});
}
Can you please assist what is wrong or what is the missing piece to get this to work?
Thanks,
Hemant.
It seems you're using commonjs as module-resolution strategy. Your import will be the problem in that case. Try changing it to:
const dbConn = require('./utils/db/dbConn').dbConn;
or
const { dbConn } = require('./utils/db/dbConn');
or
import {dbConn } from './utils/db/dbConn';
Here's a simple example to show what's going on. Consider this simple ts-class:
export class TestClass {
static test():void {
console.log("it works")
}
}
It will be transpiled into:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestClass = void 0;
class TestClass {
static test() {
console.log("in workds");
}
}
exports.TestClass = TestClass;
//# sourceMappingURL=index.js.map
If you then require this with const TestClassModule = require('./test-class');, TestClassModule will yield:
{ TestClass: [Function: TestClass] }
Hence, you need to use const { TestClass } = require('./test-class');.

How can i use custom logger in fastify?

My company have a custom developed logger package, and we want to use that as default logger in fastify. I tried to understand how to register my logger with this simple example below, but fastify always use Pino.
index.js
const log = require("./Logger");
const fastify = require("fastify")({ logger: log });
fastify.get("/", (request, reply) => {
request.log(
"includes request information, but is the same logger instance as `log`"
);
reply.send({ hello: "world" });
});
fastify.listen(3000)
logger.js
function Logger(...args) {
this.args = args;
}
Logger.prototype.info = function(msg) {
console.log("myLogger", msg);
};
logger.js also contains error, debug, fatal, warn, trace, child functions but the functions body is same.
The result is:
{"level":30,"time":1553095994942,"msg":"Server listening at http://127.0.0.1:3000","pid":14543,"hostname":"VirtualBox","v":1}
whitch is the default Pino output.
as explained here, your logger
must have the following methods
So this example works:
function Logger(...args) {
this.args = args;
}
Logger.prototype.info = function (msg) { console.log("myLogger", msg); };
Logger.prototype.error = function (msg) { console.log("myLogger", msg); };
Logger.prototype.debug = function (msg) { console.log("myLogger", msg); };
Logger.prototype.fatal = function (msg) { console.log("myLogger", msg); };
Logger.prototype.warn = function (msg) { console.log("myLogger", msg); };
Logger.prototype.trace = function (msg) { console.log("myLogger", msg); };
Logger.prototype.child = function () { return new Logger() };
const myLogger = new Logger()
const app = require('fastify')({
logger: myLogger
})
app.get("/", (request, reply) => {
request.log.info('hi');
reply.send({ hello: "world" });
});
app.listen(3000)
Here you can check the logger validation applied to your parameter
in case you need log4js,
this is a customized logger that supports json event.
you c
import log4js from "log4js";
import {log4jsConfig} from "./log4js.config.js";
log4js.addLayout('json', function (config) {
return function (logEvent) {
logEvent.application = 'my-app';
logEvent.processId = process.pid;
if (process.env.IP) {
logEvent.ip = process.env.IP;
}
if (process.env.ENV) {
logEvent.ENV = process.env.ENV;
}
if (logEvent.data && logEvent.data.length > 0) {
logEvent.message = logEvent.data[0];
delete logEvent.data;
}
return JSON.stringify(logEvent);
};
});
const log = log4js.getLogger('engine');
log4js.configure(log4jsConfig);
export class AndromedaLogger {
logger;
loggerOptions;
constructor(args) {
this.logger = log;
}
get Logger() {
return this.logger;
}
static configGlobal(options) {
this.loggerOptions = options;
}
info(message) {
this.logger.info(message);
}
error(message, trace) {
this.logger.error(`${message} -> (${trace || 'trace not provided !'})`);
}
warn(message) {
this.logger.warn(message);
}
debug(message, context) {
this.logger.debug(message);
}
trace(message, context) {
this.logger.trace(message);
}
fatal(message, context) {
this.logger.fatal(message);
}
child() {
return new AndromedaLogger()
};
}
log4js config:
const _log4jsConfig = {
appenders: {},
categories: {
default: {
appenders: [],
level: 'trace',
},
},
};
_log4jsConfig.appenders.stdout = {
type: 'stdout',
layout: { type: 'colored' },
};
_log4jsConfig.appenders['file'] = {
type: 'file',
filename: 'logs/app.log',
maxLogSize: 104857600,
numBackups: 3,
};
_log4jsConfig.categories.default.appenders.push('stdout');
_log4jsConfig.categories.default.appenders.push('file');
export const log4jsConfig = _log4jsConfig;

function worked automatically (constructor) when file required in 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?

Hijack response before sent to client

I have follow codes to be used as middlewares
module.exports=function(callback) {
callbacks.push(callback);
return function(req,res,next) {
if (!res.hijacked) {
res.hijacked=true;
} else {
return next();
}
var send=res.send;
res.send=function(data) {
var body=data instanceof Buffer ? data.toString() : data;
var requests=[];
requests.push(function(next) {
callbacks[0](req,res)(body,doneWrapper(body,next));
});
for (var i=1;i<callbacks.length;i++) {
var hijackCallback=callbacks[i];
requests.push(function(result,next) {
hijackCallback(req,res)(result,doneWrapper(result,next));
});
}
var that=this;
async.waterfall(requests,function(err,result) {
send.call(that,result);
requests=null;
body=null;
that=null;
});
};
next();
};
};
An example of usage is as following:
module.exports=function() {
return hijack(function() {
return function(result,done) {
var json={};
try {
json=JSON.parse(result);
} catch(e) {
return done();
}
if (!_.isArray(json)) {
return done();
}
var sorted=_(json).sortBy(function(item) {
if (_.isObject(item.information)) {
return item.information.rangeIndex1 || 999;
} else {
return 1001;
}
});
done(sorted);
}
});
};
It worked fine initially as middlewares in routes.
However,When i try to make it as app.use(hijackMiddleware()). Something went wrong, I got this Can't set headers after they are sent error.
There is no problem when used as middlewares in routes,though.
Have you consider using express-interceptor? Is really easy to use:
var express = require('express');
var cheerio = require('cheerio');
var interceptor = require('express-interceptor');
var app = express();
var finalParagraphInterceptor = interceptor(function(req, res){
return {
// Only HTML responses will be intercepted
isInterceptable: function(){
return /text\/html/.test(res.get('Content-Type'));
},
// Appends a paragraph at the end of the response body
intercept: function(body, send) {
var $document = cheerio.load(body);
$document('body').append('<p>From interceptor!</p>');
send($document.html());
}
};
})
// Add the interceptor middleware
app.use(finalParagraphInterceptor);
app.use(express.static(__dirname + '/public/'));
app.listen(3000);

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