I'm using module.exports to export list of module. Below is my
Models/Message.js
var msgModel = function()
{
var getMsg = function() {
return "Hello World";
}
return{
getMsg : getMsg
}
}
module.exports = msgModel;
Below is my app.js
var msgModel = require('./Models/Message');
console.log(msgModel.getMsg());
It raised me error
console.log(msgModel.getMsg());
^
I can't figure out the problem.
You need to do console.log(msgModel().getMsg());. This is because msgModel is a function. I would suggest rewriting your msgModel like the example below instead to achieve the call you wanted.
var msgModel = {
getMsg: function() {
return "Hello World";
}
};
module.exports = msgModel;
You could do something like this:
var msg = new msgModel();
console.log(msg.getMsg());
You should export a function and invoke it in the main app.js
for example:
app.js
var msgModel = require('./Models/Message');
msgModel();
Message.js
var getMsg = function(){
console.log("Hello World!");
};
module.exports = getMsg;
You could write it this way in order to group more functions:
module.exports = {
getMsg: function() {
return "Hello World";
},
anotherFnc: function () {
...
}
};
then you could use:
var msgModel = require('./Models/Message');
console.log(msgModel.getMsg());
Related
I managed to get the data on #1, but I can't print it out in Express #2, how can I resolve the data and output it?
I believe this is my code error where I can't place the correct async/await.
Here's my code:
minify.js
const { getContent } = require('./get');
const { minifyJS } = require('./processing');
async function getMinify(req, res) {
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error.content);
}
}
module.exports = { getMinify }
get.js
const got = require('got');
async function getContent(remoteUrl) {
const code = await got(remoteUrl);
return code.body;
}
module.exports = { getContent }
processing.js
const { minify } = require('uglify-js');
async function minifyJS(data) {
const result = await minify(data);
return result.code;
}
module.exports = { minifyJS }
app.js
const express = require('express');
const { getMinify } = require('./minify');
const app = express();
app.get('/*', getMinify);
app.listen(5000);
based on uglify-js, result of minify(data) function has two object code and error.
and minify(data) don't need to await
processing.js should changed
const { minify } = require("uglify-js");
async function minifyJS(data) {
let { error, code } = minify(data);
if (error)
throw error;
return code;
}
module.exports = { minifyJS };
and for handling of two .js file (get.js and processing.js) changed
minify.js
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error); // remove .content
}
app.js
var a = function(){
var b = function(){
console.log("hello")
}
}
module.exports = {a}
index.js
console.log(require("./app.js").a().b())
I wantr to get the output "hello" but i am getting error can call property b of undefined
Please help to obtain the result
try this
var a = function () {
var b = function () {
console.log("hello")
}
return {b:b};
}
how to call methods with variable in node.js
this is so that ;
var file=require("./file");
var method=b;
file.method();
//output
//error : TypeError: file.method is not a function
how to use it?
ok try this then
//file.js
module.exports = {
index: function () {
console.log("index called");
},
index2 :function() {
console.log("index2 called");
} ,
index3 : function () {
console.log("index3 called");
}
};
then
app.get("file/:method",function (req,res)
{
var method = req.params.name;
var file = require('./file');
file[method]();
}
app.get("file/:method",function (req,res)
{
var file("./file");
var method=req.params.name;
file.method();
this is what ı want to tell
I have a funciton inside a node-module:
//app.js
var minify = require("./minify.js")(app, express);
//minify.js
module.exports = function (app, express) {
...
function fileList(dir) {
return fs.readdirSync(dir).reduce(function(list, file) {
var name = path.join(dir, file);
var isDir = fs.statSync(name).isDirectory();
var isJs = name.endsWith('.js');
return list.concat(isDir ? fileList(name) : isJs ? [name] : [] );
}, []);
}
...
};
I want to change it to a method to make my code more readable.
I tried the following, without success:
module.fileList = function(dir) {
return fs.readdirSync(dir).reduce(function(list, file) {
var name = path.join(dir, file);
var isDir = fs.statSync(name).isDirectory();
var isJs = name.endsWith('.js');
return list.concat(isDir ? fileList(name) : isJs ? [name] : [] );
}, []);
module.prototype.fileList = function(dir) {
return fs.readdirSync(dir).reduce(function(list, file) {
var name = path.join(dir, file);
var isDir = fs.statSync(name).isDirectory();
var isJs = name.endsWith('.js');
return list.concat(isDir ? fileList(name) : isJs ? [name] : [] );
}, []);
}
I expect to be able to call the method usint this.fileList or minify.filelist, depending on my scope.
You can return object from exported function:
//minify.js
function fileList(dir) {
// ...
}
module.exports = function(app, express) {
//...
return {
fileList: fileList
};
}
//app.js
var minify = require("./minify.js")(app, express);
minify.fileList();
I think this way is easier :
minify.js
module.exports = {
fileList: function(app, express) {
// your code here
},
Method2: function() {
// another code here
}
}
And you can use it like this:
app.js
var minify = require('minify');
minify.fileList(app, express);
minify.Method2();
I have a Node.js module which return memory used:
//meminfo.js
module.exports = myObj;
myObj = (function() {
var pub = {};
pub.getmem = function() {
meminfo.list(function(memused) {
return memused;
});
};
return pub;
})();
If I want to retrieve memused by this:
//main.js
var meminfo = require('./meminfo');
console.log(myObj.getmem());
Then could you give me some suggestions about how to return 'memused' from this in meminfo.js:
meminfo.list(function(memused) {
return memused;
});