Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Last active October 19, 2017 15:23
Show Gist options
  • Select an option

  • Save Sysetup/5a2f3e436813e8d49fecc7cf717e550e to your computer and use it in GitHub Desktop.

Select an option

Save Sysetup/5a2f3e436813e8d49fecc7cf717e550e to your computer and use it in GitHub Desktop.
module.exports and exports NodeJS examples
//***************************
//sayhello.js
var sayhello1 = function() { };
var sayhello2 = function() { };
exports.sayhello1 = sayhello1;
exports.sayhello2 = sayhello2;
//index.js
var x = require('./sayhello.js');
x.sayhello1();
//***************************
//sayhello.js
function sayhello3() { };
function sayhello4() { };
exports.sayhello3 = myFunc3;
exports.sayhello4 = myFunc4;
//index.js
var y = require('./sayhello.js');
y.sayhello3();
//***************************
//sayhello.js
module.exports = exports = function() {
console.log("Hello!");
};
//index.js
var sayHello = require('./sayhello.js');
sayHello(); // "Hello World!"
//***************************
//sayhello.js
exports.run = function() {
console.log("Hello guys!");
}
//index.js
var sayHello = require('./sayhello');
sayHello.run(); // "Hello World!"
//***************************
//sayhello.js
module.exports = {
sayHelloInEnglish: function() {
return "Hello";
},
sayHelloInSpanish: function() {
return "Hola";
}
};
//index.js
var sayHello = require('./sayhello');
sayHello.sayHelloInEnglish();
//***************************
//sayhello.js
module.exports = {
Hello: 'Hi!'
}
//index.js
var sayHello = require('./sayhello');
sayHello.Hello;
//***************************
//sayhello.js
var sayHello = {
'sayHello1':'Hi!',
'sayHello2':'Hello'
}
function sayHi(){
console.log('Hi!!');
return 'Hello'
}
exports.sayHello = requires;
exports.sayHi = sayHi;
//index.js
var x = require("./sayhello").sayHello.sayHello1;
var y = require("./sayhello").sayHi;
console.log(x) //Hi!
console.log(y) //function sayHi(){
//console.log('Hi!!');
//return 'Hello'
//}
console.log(y()) //Hi!!
//Hello
//***************************
//require.js
var x = 5,
z = 10;
exports.w = function(){
return x + z //Function exported, takes variables x and z, makes sum and export perfectly the result.
}
//index.js
var y = require('./require');
console.log('y: '+y.w());
//***************************
//person.js
var name = 'yyy';
var age = 25;
var sex = 'F'; //Private, not exported.
function speak(name, age, sex){
return 'My name is: '+name+' I am '+age+' years old and my sex is '+ sex; //It doesn't print sex var from external module.
}
function smile(){
var smile_1 = 'I like smile!'
return smile_1+' Ja';
}
function sing(){
return 'La la la';
}
speak(name,age,sex); //Called function inner module is not executed in exported module.
console.log('Log function from module: ' + speak(name,age,sex)); //console.log is regardless printed if the function is or not exported.
console.log('Log variable from module: ' + name); //console.log is regardless printed if the function is or not exported.
module.exports = {
name:name,
age:age,
//sex:sex, //sex is not exported.
speak:speak,
smile:smile,
sing:sing
}
//index.js
var person = require('./person');
console.log('Log from index: ' + person.speak(person.name,person.age,person.sex) + '\n'); // sex is not exported from module thus is not printed.
console.log('Log from index: ' + person.smile());
console.log('Log from index: ' + person.sing());
//***************************
//errorHandle.js
module.exports = function (err, req, res, next) {
const error = {};
error.msg = err.msg || err.message || 'Unknown reason';
const status = err.status || '400';
return res.status(status).send(error);
}
//index.js
const errorHandle = require('./errorHandle');
console.log(errorHandle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment