0 votes
1 view
in Web Technology by (11.2k points)

app.js

var http = require('http');

const module1 = require('./module1');

function onRequest(request,response){

    response.writeHead(200,{'Content-type': 'text/plain'});

    response.write(module1.myStr);

    //module1.myfun();

    response.end();

}

http.createServer(onRequest).listen(8000);

module1.js

function myfun(){

    console.log("fun is called");

}

var myStr = 'String!!'

module.exports.myfun = myfun;

module.exports.myStr = myStr;

I have followed nodejs basic tutorial by academind and wrote down the exact same code.

1 Answer

0 votes
by (22.1k points)

Both the files should be in same folder also your code should look like this:

app.js

var http = require('http');

const module1 = require('./module1');

function onRequest(request,response){

    response.writeHead(200,{'Content-type': 'text/plain'});

    response.write(module1.myStr);

    //module1.myfun();

    response.end();

}

http.createServer(onRequest).listen(8000);

module1.js

function myfun() {

  console.log("fun is called");

}

var myStr = "String!!";

module.exports.myfun = myfun;

module.exports.myStr = myStr;

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...