[英]Node.js. Error: module is not a function
i have a main file -- index.js: 我有一个主文件 - index.js:
var express = require('express');
var app = express();
var request = require('request');
var demo = require('demo');
// This app will only respond requests to the '/scrape' URL at port 3000.
app.get('/scrape', function (req, res) {
var url = "http://www.l.com";
request(url, function (error, response, html) { // two parameters: an URL and a callback
if (!error) {
demo(html);
}
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
and my module is demo.js: 我的模块是demo.js:
module.exports = function (html) {
....
return JSON.stringify(json);
}
The error is : 错误是:
TypeError: demo is not a function TypeError:demo不是函数
I am new to node.js, i would like to know why this didn't work. 我是node.js的新手,我想知道为什么这不起作用。 Maybe i dont understand the real principle of module? 也许我不明白模块的真正原理? Thank you for answer me first. 谢谢你先回答我。
You're not exporting your module properly. 您没有正确导出模块。 It should be: 它应该是:
exports.demo = function ....
尝试在index.js中包含您的演示模块:
var demo = require('./demo.js');
For the other freshers who use module in node.js for the first time. 对于第一次在node.js中使用模块的其他新手。
first, made a new module called the name of your module.js Second, it is not necessary to do " npm install demo --save
", if you want, it is also okay. 首先,制作了一个名为module.js名称的新模块。其次,没有必要做“ npm install demo --save
”,如果你愿意的话,也没关系。 Third, in the main js which u want to call this module, focus on the name and the path of the module, you should write var anyName = require('the name of your module');
第三,在你想要调用这个模块的主要js中,关注模块的名称和路径 ,你应该写var anyName = require('the name of your module');
, if they are in the same directory, you should write like this: var anyName = require('./the name of your module'); ,如果它们在同一个目录中,你应该这样写:var anyName = require('./你的模块的名字');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.