Node.js. Error: module is not a function

I have a main file - 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:

module.exports = function (html) {
    ....

    return JSON.stringify(json);
}

Mistake:

TypeError: demo is not a function

I am new to node.js, I would like to know why this did not work. Maybe I do not understand the true principle of the module? Thanks for answering me first.

+4
source share
3 answers

For other freshers who use the module in node.js for the first time.

, module.js -, "npm install demo --save", , . -, js, , , var anyName = require('the name of your module');, , : var anyName = require ('./ ');

0

. :

exports.demo = function ....
0

Try turning on the demo module in index.js:
var demo = require('./demo.js');

0
source

Source: https://habr.com/ru/post/1610171/


All Articles