22
loading...
This website collects cookies to deliver better user experience
const module_name=require('module_name');
const myvariable = require('http');
//Example: Load and Use Core http Module
const http = require('http');// 1. Import Node.js core module
const server = http.createServer(function(req, res){// 2. Creating Server
//handle incoming requests here.
});
server.listen(5000); // 3. Listen for any incoming reuqests.
Filename:calc.js
exports.add=function(x,y){
return x+y;
};
exports.sub=function(x,y){
return x-y;
};
exports.multi=function(x,y){
return x*y;
};
exports.div=function(x,y){
return x/y;
};
Filename:app.js
let calculator=require('./calc');
let x=10,y=20;
console.log("Addition of 10 and 20 is "+calculator.add(x,y));
NB
:You must specify ./ as a path of the root folder to import a local module.However,you don't need to specify the path to import Node.js core modules.
require()
function returns a calculator object because calculating module exposes an object in calc.js
using module.exports or exports. So now you can use calculating module as an object and call any of its function using dot notation e.g calculator.add(x,y)
or calculator.sub(x,y)
or calculator.div(x,y)
node app
Addition of 10 and 20 is 30
npm install express
const express = require('express');