I have been trying to understand, module.export how to pass a parameter with it. Now I have made a demo server for testing it.
File - index.js
var express = require('express');
var check = require('./file');
var app = express();
app.get('/',check.fun1("calling"),(req,res)=>{
res.send('here i am');
})
app.listen(3000,()=>{
console.log("Server is up");
})
With a middleware check,
module.exports = fun1 = name => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
module.exports = fun2 = name2 => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
Now this is not working, but when I change it in, its start to work
fun1 = name => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
fun2 = name2 => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
module.exports = {
fun1,
fun2
};
Now, this might look like a stupid question, that if it's working then why I am asking but, what change should I make, in index.js file so that my first type of module.export starts working. It's just all out of sheer curiosity
Thanks
module.exports = fun1 = name => {...}to do? What's the purpose offun1here? If you want to export two functions from your module, then you writeexports.fun1 = name => {...}andexports.fun2 = name2 => {...}. You only overwritemodule.exportsif you want to export a single value. Not sure how the title of the questions relates to your issue.exportsis directly converted to module.export hmmm.. thankxexportsis simply a variable in scope of a module and initially references the same value asmodule.exports.