call a function from one file in node.js - node.js

i have a file named test2.js created a function inside it
var schedule = require('node-schedule');
var jobScedule = function(time, jobid) {
schedule.scheduleJob(time, function(jobid){
console.log("scheduling starts");
});
}
exports.jobScedule = jobScedule;
in test1.js i have to call this function.
var objTest2 = require("./Test2.js");
var time = new Date();
var jobid=10;
objTest.jobScedule(time,jobid)
console.log(time);
console.log(jobid);
i dont know to call function from another file in node.js.rectify me

var objTest= require("./Test2.js");
var time = new Date();
var jobid=10;
objTest.jobScedule(time,jobid)
console.log(time);
console.log(jobid);

In 'user.js', contains functions to be exported like
module.exports = {
func1: function () {
// func1
},
func2: function () {
// func2
}
};
In the other admin.js files, you can include the 'user.js' by
const user = require('./user');
In the following code, the exported funct1 can be like
user.func1();

Related

How to properly use Mongoose models with Node.js?

I'm trying to use mongoose to control my db logic and transactions. I already got Schema definitions and I'm exporting the models.
Howver when i try to use a model, it will fail witl a message like:
return mongoose.model('Report', reportSchema);
} has no method 'find'...
This is my Model export:
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var reportSchema = mongoose.Schema({
category: ObjectId,
authorName: String,
authorEmail: String,
text: String,
address: String,
coordinates: {
type: "Point",
coordinates: [Number,Number]
},
date: {
type: Date,
default: new Date()
},
comments: Array
});
return mongoose.model('Report', reportSchema);
});
And this is how my controller functions are coded using mongoose inside:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report');
var Category = require('../models/Category');
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getReport (request,response,next) {
var id = request.params.id;
Report.findById({_id: id}, function (err,report) {
callback(err,report);
});
}
function _createReport (request,response) {
var newReport = new Report();
newReport.text = request.body.text;
newReport.category = request.body.category;
newReport.author = request.session.userId;
newReport.save(function (err,savedReport) {
callback(err,savedReport._id,response);
});
}
function _updateReport (request,response) {
var id = request.params.id;
var update = request.body.report;
Report.findByIdAndUpdate(id, update, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _deleteReport (request,response) {
var id = request.params.id;
Report.findByIdAndRemove(id, function (err,foundReport) {
callback(err,foundReport,response);
});
}
function _getReports (request,response,next) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports){
callback(err,reports,response);
});
}
function _getCategories (request,response) {
var take = request.query.take;
var skip = request.query.skip;
Report.find({}).limit(take).skip(skip).exec(function (err,reports) {
callback(err,reports,response);
});
}
function _getCategoryReports (argument) {
var _id = mongoose.Types.ObjectId(request.params.id);
Report.find({category:id},{category:false},function (err, foundReports) {
callback(err,foundReports,response);
});
}
function _createCategory (request,response) {
var newCategory = new Category();
newCategory.name = request.body.name;
newCategory.save(function (err,savedCategory) {
callback(err,savedCategory._id,response);
});
}
function _updateCategory (request,response) {
var id = request.params.id;
var update = request.body.category;
Category.findByIdAndUpdate(id, update, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function _deleteCategory (request,response) {
var id = request.params.id;
Category.findByIdAndRemove(id, function (err,foundCategory) {
callback(err,foundCategory,response);
});
}
function callback (err,object,response) {
if (err)
response.status(500).send(JSON.stringify(err));
response.send(JSON.stringify(object));
}
var apiController = {
getReports: _getReports,
getReport: _getReport,
createReport: _createReport,
updateReport: _updateReport,
deleteReport: _deleteReport,
getCategories: _getCategories,
getCategoryReports: _getCategoryReports,
createCategory: _createCategory,
updateCategory: _updateCategory
}
return apiController;
})();
Before this, a mongoose connection is ensured:
var connectToMongoose = function (mongoose,app) {
var connect = function () {
var options = { server: { socketOptions: { keepAlive: 1 } } };
mongoose.connect( 'mongodb://localhost/data4', options);
}
mongoose.connection.on('connected', function () {
console.log('Connected to db');
app.listen(32884, function() {
console.log("Listening at \"data4 port\" #:32884");
});
})
mongoose.connection.on('error', function (err) {
console.log(err);
});
mongoose.connection.on('disconnected', function () {
console.log('Disconnected from db, attempting to connect again...');
app.close();
connect();
});
connect();
};
module.exports = connectToMongoose;
Which is invoked by require('./db/mongoose-connect.js')(mongoose,app);
What am I doing wrong?
There are a couple issues here that I caught off the bat.
First off, I don't see a mongoose.connect() line that explicitly connects your mongoose ODM to a mongo server+database. An example would be:
var mongoose = require( 'mongoose' ),
Schema = mongo.Schema,
ObjectId = mongo.Schema.ObjectId;
mongoose.connect( 'mongodb://localhost/db_name' );
Your schema export looks fine. But you're using an anonymous function as your export. Since you're doing that, your require statement needs to change a little:
var Report = require('../models/Report')();
var Category = require('../models/Category')();
Notice the () at the end of the require statements. You need to execute the function that you're defining as your model file's module.export.
EDIT: I see that you added your mongoose connect code. At this point, executing the module.exports function that you assign in the model file should allow your mongoose models to function as intended.
When you export a function;
// file: A.js
module.exports = function () {
//some logic
};
And you want to use it on another file, when you require the A file, you are importing a function and in order to use that function, you need to to invoke it.
// file: B.js
var A = require('./A.js');
A();
so your model is exporting a function
module.exports = (function() {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// ..
// some code
// ..
return mongoose.model('Report', reportSchema);
});
and when you are importing your model from your controller, you need to execute your imported function so that your Report variable contains the model created:
module.exports = (function() {
var mongoose = require('mongoose');
var Report = require('../models/Report') ();
I have created a gist of how you could write your code using modules without using IIFE.
https://gist.github.com/wilsonbalderrama/d5484f3f530899f101dc
actually if you download all those files on a folder and run:
$ sudo npm install
$ mocha
You could see that all the tests created for the controller are passing.
In addition you don't need to use IIFE in Node.JS since when you are creating a module because you already have an isolated scope in Node.JS using modules.
// IIFE
module.exports = (function() {
var apiController = {
getReport: function () {}
}
return apiController;
})();
In Node.JS you can export a object,
// GOOD WAY
module.exports = {
getReport: function () {}
};

NodeJS - How to test index.js without module.exports

I'm testing my NodeJs project using Mocha and I have a file, index.js that is the main file without module.exports that is run like a CLI
index.js
// CLI tools
var bluebird = require('bluebird');
var gigatool = require('./lib/gigatool');
var debug = require('debug')('index');
var size = 20;
var page = process.env.BATCH;
var startDate = process.env.START;
var dataDir = process.env.DATADIR;
debug(page, startDate, dataDir);
// requires parameters
if (!process.env.BATCH) {
throw new Error('BATCH environment variable is needed');
}
tool = gigatool(size, page, dataDir);
bluebird.all([tool.clean(startDate), tool.continuous()])
.finally(function(){
process.exit(0);
});
test.js
'use strict';
var chai = require('chai');
var fs = require('fs');
var noop = require('lodash.noop');
var rimraf = require('rimraf');
var async = require('async');
var rimraf = require('rimraf');
var expect = chai.expect;
describe.only('Integration', function() {
var dataDir = './countries';
var path = dataDir + '/Albania';
describe('clean run', function() {
this.timeout(10000);
before(function() {
process.env.BATCH = 1;
process.env.DEBUG = '*';
require('../../index');
});
after(function(done) {
// rimraf(dataDir, done);
});
});
});
if I run require('./index'), it will run the module and then continue to move forward, how can i wait for it to end before i run test cases?
Note: It is calling some apis
You need to test your whole application at once, this is still testing but hardly "unit" testing unless your code is a unit ("the unix way"). For this reason your code should start with:
var Promise= require("bluebird");
var exec= Promise.promisify(require("child_process").exec);
var run = function(args){
return exec("node", ["../../index.js"].concat(args)).get("stdout");
};
Which would make your tests test the actual inputs on the file:
describe('your code', function() {
it('should work with params a,b', function(){
return run(['a','b']).then(function(out){ // note the Mocha promise syntax
assert.equal(out, 'your expected stdout');
});
});
});
Unfortunately, there is no way to unit test individual aspects of a CLI Node script as you have it. Instead, what I've done in the past is have conditional execution based on whether the script was used via require or called from the command line:
// index.js
var foo = require('foo');
var bar = require('bar');
// ...
// determine if this script is being required as a module or is CLI
var IS_EXECUTING = (require.main === module);
var methods = {
init: function(args) {
methods.auditArgs(args);
methods.doSomeStuff(arg1, arg2);
methods.doOtherStuff();
},
auditArgs: function(args) {/* ... */},
doSomeStuff: function(arg1, arg2) {/* ... */},
// ...
};
// At the bottom we either begin execution or return a function which can
// be called in a test harness when ready...
if (IS_EXECUTING) {
methods.init(process.argv);
} else {
module.exports = function (mockMethods) {
// you could have some code here to mock out provided methods
// for example:
methods.auditArgs = mockMethods.auditArgs || methods.auditArgs;
// then return the "API" for this script...
return methods;
};
}
In your test harness then you would simply require the file and when ready, use it like you would any other module. But when called from the command line the code will just execute normally:
// in test.js
'use strict';
var chai = require('chai');
// ...
var appFactory = require('index');
var expect = chai.expect;
describe('initialization', function() {
var app;
beforeEach(function() {
app = appFactory({
auditArgs = chai.spy(function() { });
// other mock method implementations, spies, etc
});
});
it('should call necessary methods on init', function() {
expect(app.auditArgs).to.have.been.called(1);
// ...
});
});

Module exports and method call

I'm trying to developp a module with nodejs. My code is something like this :
var fs = require('fs');
module.exports.method1 = function () {
// Some stuff
}
module.exports.method2 = function ()
{
// Some stuff
}
I would like to do something like :
module.exports.method2 = function (url, dir, name)
{
this.method1();
}
How to do this ?
var fs = require('fs');
exports.method1 = function () {
// Some stuff
}
exports.method2 = function ()
{
exports.method1();
}
To make it a little bit swankier you could do something like:
module.exports = {
function1: function(){
//some stuff
},
function2: function(){
this.function1();
}
};
If you want private scope:
module.exports = function(){
var myPrivateVar = 'foo';
var publicObject = {
function1: function(){
//some stuff
console.log(myPrivateVar);
},
function2: function(){
this.function1();
}
};
return publicObject;
}
The difference is though that you need to invoke it where the former is just an object reference. The second example is more like a constructor function... require + invoke... var myMod = myModule(); myMod.function2() will output 'foo'
IMHO this is a more object-oriented way of doing it rather than exporting each individual function. This allows for better separation and cleaner code.

How to initialize in module pattern

How can i initialize async.queue "for" the global module scope?
The example bellow shows the main problem, that qq is undefined, not yet known or its only defined locally in a function scope.
The target is to access a "module-global" q in different module member functions.
So creating a module pattern version of the example in https://github.com/caolan/async#queue
I know why the // not working-code isn't valid, it is only too show which declaration ideas i tried.
Additional i m aware of how to solve the problem by using a different pattern, but that wouldn't answer the question.
var mymodule = (function() {
'use strict';
var async = require('async');
// var q = async.queue(mymodule.qq); // not working
// var q ; // not working
var mymodule = {
// q = async.queue(this.qq); // not working
init: function() {
// var q = async.queue(this.qq); // local not global
// q = async.queue(this.qq); // not working
q.drain = function() {
console.log('all items have been processed');
}
},
add: function(task) {
this.q.push(task);
},
qq: function(task, callback) {
console.log(task);
callback();
},
};
return mymodule;
}());
'use strict';
var async = require('async');
var mymodule = function(){
//This will be you constructor
//You can do something like this
this.queue = async.queue(function(task, callback){
console.dir(task);
}, 4);
};
//Now start adding your methods
mymodule.prototype.add = function(task){
this.queue.push(task, function(){});
};
mymodule.prototype.qq = function(task, callback){
// ..
callback()
};
//export it
module.exports = mymodule;

Declare multiple module.exports in Node.js

What I'm trying to achieve is to create one module that contains multiple functions in it.
module.js:
module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); },
// This may contain more functions
main.js:
var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);
The problem I have is that the firstParam is an object type and the secondParam is a URL string, but when I have that it always complains that the type is wrong.
How can I declare multiple module.exports in this case?
You can do something like:
module.exports = {
method: function() {},
otherMethod: function() {},
};
Or just:
exports.method = function() {};
exports.otherMethod = function() {};
Then in the calling script:
const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');
To export multiple functions you can just list them like this:
module.exports = {
function1,
function2,
function3
}
And then to access them in another file:
var myFunctions = require("./lib/file.js")
And then you can call each function by calling:
myFunctions.function1
myFunctions.function2
myFunctions.function3
in addition to #mash answer I recommend you to always do the following:
const method = () => {
// your method logic
}
const otherMethod = () => {
// your method logic
}
module.exports = {
method,
otherMethod,
// anotherMethod
};
Note here:
You can call method from otherMethod and you will need this a lot
You can quickly hide a method as private when you need
This is easier for most IDE's to understand and autocomplete your code ;)
You can also use the same technique for import:
const {otherMethod} = require('./myModule.js');
module.js:
const foo = function(<params>) { ... }
const bar = function(<params>) { ... }
//export modules
module.exports = {
foo,
bar
}
main.js:
// import modules
var { foo, bar } = require('module');
// pass your parameters
var f1 = foo(<params>);
var f2 = bar(<params>);
This is just for my reference as what I was trying to achieve can be accomplished by this.
In the module.js
We can do something like this
module.exports = function ( firstArg, secondArg ) {
function firstFunction ( ) { ... }
function secondFunction ( ) { ... }
function thirdFunction ( ) { ... }
return { firstFunction: firstFunction, secondFunction: secondFunction,
thirdFunction: thirdFunction };
}
In the main.js
var name = require('module')(firstArg, secondArg);
If the files are written using ES6 export, you can write:
module.exports = {
...require('./foo'),
...require('./bar'),
};
One way that you can do it is creating a new object in the module instead of replacing it.
for example:
var testone = function () {
console.log('test one');
};
var testTwo = function () {
console.log('test two');
};
module.exports.testOne = testOne;
module.exports.testTwo = testTwo;
and to call
var test = require('path_to_file').testOne:
testOne();
You can write a function that manually delegates between the other functions:
module.exports = function(arg) {
if(arg instanceof String) {
return doStringThing.apply(this, arguments);
}else{
return doObjectThing.apply(this, arguments);
}
};
There are multiple ways to do this, one way is mentioned below.
Just assume you have .js file like this.
let add = function (a, b) {
console.log(a + b);
};
let sub = function (a, b) {
console.log(a - b);
};
You can export these functions using the following code snippet,
module.exports.add = add;
module.exports.sub = sub;
And you can use the exported functions using this code snippet,
var add = require('./counter').add;
var sub = require('./counter').sub;
add(1,2);
sub(1,2);
I know this is a late reply, but hope this helps!
use this
(function()
{
var exports = module.exports = {};
exports.yourMethod = function (success)
{
}
exports.yourMethod2 = function (success)
{
}
})();
also you can export it like this
const func1 = function (){some code here}
const func2 = function (){some code here}
exports.func1 = func1;
exports.func2 = func2;
or
for anonymous functions like this
const func1 = ()=>{some code here}
const func2 = ()=>{some code here}
exports.func1 = func1;
exports.func2 = func2;
You can use like i did below... for both functions and arrow functions :
greet.js :
function greetFromGreet() {
console.log("hello from greet module...");
}
const greetVar = () => {
console.log("greet var as a arrow fn/...");
};
module.exports = { greetVar, greetFromGreet }; // ---- multiple module export...
// -----------------------------------------------
app.js :
const greetFromGreets = require("./greet");
greetFromGreets.greetFromGreet();
greetFromGreets.greetVar();
// -----------------------------------------------
Inside your node module you can export various functions such as:
module.exports.eat = eat;
function eat() {
.......
return *something*;
};
module.exports.sleep = sleep;
function sleep() {
.......
return *something*;
};
Note that you are not calling the functions while exporting them.
Then while requiring the modules you can require as:-
const task = require(__dirname + "/task.js");
//task is the name of the file
let eat = task.eat();
let sleep = task.sleep();
Two types module import and export.
type 1 (module.js):
// module like a webpack config
const development = {
// ...
};
const production = {
// ...
};
// export multi
module.exports = [development, production];
// export single
// module.exports = development;
type 1 (main.js):
// import module like a webpack config
const { development, production } = require("./path/to/module");
type 2 (module.js):
// module function no param
const module1 = () => {
// ...
};
// module function with param
const module2 = (param1, param2) => {
// ...
};
// export module
module.exports = {
module1,
module2
}
type 2 (main.js):
// import module function
const { module1, module2 } = require("./path/to/module");
How to use import module?
const importModule = {
...development,
// ...production,
// ...module1,
...module2("param1", "param2"),
};
module1.js:
var myFunctions = {
myfunc1:function(){
},
myfunc2:function(){
},
myfunc3:function(){
},
}
module.exports=myFunctions;
main.js
var myModule = require('./module1');
myModule.myfunc1(); //calling myfunc1 from module
myModule.myfunc2(); //calling myfunc2 from module
myModule.myfunc3(); //calling myfunc3 from module
Use the export keyword
module.js
export {method1, method2}
And import them in main.js
import {method1, method2) from "./module"
If you declare a class in module file instead of the simple object
File: UserModule.js
//User Module
class User {
constructor(){
//enter code here
}
create(params){
//enter code here
}
}
class UserInfo {
constructor(){
//enter code here
}
getUser(userId){
//enter code here
return user;
}
}
// export multi
module.exports = [User, UserInfo];
Main File: index.js
// import module like
const { User, UserInfo } = require("./path/to/UserModule");
User.create(params);
UserInfo.getUser(userId);
You can use this approach too
module.exports.func1 = ...
module.exports.func2 = ...
or
exports.func1 = ...
exports.func2 = ...
Adding here for someone to help:
this code block will help adding multiple plugins into cypress index.js
Plugins -> cypress-ntlm-auth and cypress env file selection
const ntlmAuth = require('cypress-ntlm-auth/dist/plugin');
const fs = require('fs-extra');
const path = require('path');
const getConfigurationByFile = async (config) => {
const file = config.env.configFile || 'dev';
const pathToConfigFile = path.resolve(
'../Cypress/cypress/',
'config',
`${file}.json`
);
console.log('pathToConfigFile' + pathToConfigFile);
return fs.readJson(pathToConfigFile);
};
module.exports = async (on, config) => {
config = await getConfigurationByFile(config);
await ntlmAuth.initNtlmAuth(config);
return config;
};
module.exports = (function () {
'use strict';
var foo = function () {
return {
public_method: function () {}
};
};
var bar = function () {
return {
public_method: function () {}
};
};
return {
module_a: foo,
module_b: bar
};
}());

Resources