0

I am using NodeJS. I am receiving error while running the Node Server. I am running Node from server.js and calling a function which is present in status.js .

server.js :-

const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const getHttpsRequests = require("./status");


const app = express();
const server = new http.Server(app);
let interval;

server.listen(3000, () => {
    console.log("Server is listening on port 3000");
});


server.on('listening', () => {
    interval = setInterval(() => {
        getHttpsRequests(); // call the function getHttpsRequests from status.js
    }, 1000);
});

status.js :-

var https = require('https');

module.exports = function getHttpsRequests (https) {

    https.get('google.com', function (res) {
        console.log("statusCode: ", res.statusCode);
        console.log("headers: ", res.headers);

       res.on('data', function (d) {
            process.stdout.write(d);
        });

    }).on('error', function (e) {
        console.error(e);
    });
}

I have installed the required packages :-

npm install express body-parser http --save

I am running the node server like,

node server.js

It is giving me error :-

https.get('google.com', function (res) {
          ^

TypeError: Cannot read property 'get' of undefined

2 Answers 2

5

You are expecting https argument in getHttpsRequest, but you don't pass that to the function and hence its gives you undefined inside the function even though you have exported it externally. Either you remove that argument or name it differently

var https = require('https');

module.exports = function getHttpsRequests (http) {

    https.get('google.com', function (res) {
        console.log("statusCode: ", res.statusCode);
        console.log("headers: ", res.headers);

       res.on('data', function (d) {
            process.stdout.write(d);
        });

    }).on('error', function (e) {
        console.error(e);
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

@RaviMariya its not a typo, as he has said in answer that he has named the argument differently
oh i see my bad :D
0

In your getHttpsRequests, you're passing https argument which will override the parent https. Remove https argument from your function,

module.exports = function getHttpsRequests (){

    https.get('google.com', function (res) {
        console.log("statusCode: ", res.statusCode);
        console.log("headers: ", res.headers);

       res.on('data', function (d) {
            process.stdout.write(d);
        });

    }).on('error', function (e) {
        console.error(e);
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.