Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k 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 (25.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;

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...