let func = (a) => a + a
let obj = {}
let x = 0
export {func, obj, x}
module.exports = {
method: function() {},
otherMethod: function() {},
};
const myModule = require('./myModule.js');
const method = myModule.method;
const otherMethod = myModule.otherMethod;
// OR:
const {method, otherMethod} = require('./myModule.js');
// user.js
const getName = () => {
return 'Jim';
};
const getLocation = () => {
return 'Munich';
};
const dateOfBirth = '12.01.1982';
exports.getName = getName;
exports.getLocation = getLocation;
exports.dob = dateOfBirth;
// index.js
const user = require('./user');
console.log(
`${user.getName()} lives in ${user.getLocation()} and was born on ${user.dob}.`
);
//2 ways of module.exports
// 1
function celsiusToFahrenheit(celsius) {
return celsius * (9/5) + 32;
}
module.exports.celsiusToFahrenheit = celsiusToFahrenheit;
//2
module.exports.fahrenheitToCelsius = function(fahrenheit) {
return (fahrenheit - 32) * (5/9);
};
mycoolmodule/index.js
module.exports = "123";
routes/index.js
var mymodule = require('mycoolmodule')
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
console.log(mymodule);
});
/*will show 123 in the terminal*/
module.exports = router;
// user.js
exports.getName = () => {
return 'Jim';
};
exports.getLocation = () => {
return 'Munich';
};
exports.dob = '12.01.1982';
// index.js
const { getName, dob } = require('./user');
console.log(
`${getName()} was born on ${dob}.`
);