Node.js is a runtime environment for executing JavaScript code on the server side. It allows to use JavaScript for both client-side and server-side scripting, making it possible to build full-stack applications with a single programming language.
npm init
npm i express --save
npm i -g nodemon
required only while development : eg nodemon
npm install --save-dev nodemon
object1={
name: "Mahima",
developer: true
}
module.exports=object1;
const object2 = require ("./second.js")
console.log("Hello world",object2)
//module wrapper function
(function(exports,require,module,__filename,__dirname)
{//object
object1={
name: "Mahima",
developer: true
}
module.exports=object1;
})
console.log(exports,require,module,__filename,__dirname)
const os=require('os')
console.log(os.freemem())
console.log(os.homedir())
console.log(os.hostname())
console.log(os.EOL)
console.log(os.availableParallelism())
console.log(os.arch())
console.log(os.constants)
console.log(os.cpus())
console.log(os.devNull)
console.log(os.endianness())
console.log(os.freemem())
console.log(os.homedir())
console.log(os.hostname())
console.log(os.loadavg())
console.log(os.machine())
console.log(os.networkInterfaces())
console.log(os.platform())
console.log(os.release())
console.log(os.tmpdir())
console.log(os.totalmem())
console.log(os.type())
console.log(os.uptime())
console.log(os.version())
In Node.js, I/O operations can be performed asynchronously, meaning that they don’t block the execution of the program. Instead, they are placed in a queue, and the program continues executing other tasks. When the I/O operation is completed, a callback function is called to handle the result. This is particularly useful for I/O operations that may take a significant amount of time.
const fs= require('fs');
//async
//non blokcing i/0 thatswhy runs at last as ti takes moer time
fs.readFile('test.txt','utf8',(err,data)=>{
console.log(err,data)
})
console.log("YO line suuru ma print hucnha ani callback pachi huncha due to time: sabei run bayesi run hucnha")
fs.writeFile("test.txt","mahima is good girl",()=>{
console.log("Yo callback write garisakesi run huncha: written to file")
})
Synchronous I/O operations block the execution of the program until the operation is finished. In Node.js, synchronous operations should be used sparingly, especially for file I/O, as they can lead to performance issues and block the event loop.
const a=fs.readFileSync("test.txt") //nodejs intetntionally blocks
console.log(a.toString())
console.log("yo chai last mei print hucnha")
fs.writeFileSync("test.txt","mahima is good girl",()=>{
console.log("Yo sync ho: intentionally process block hunchaa")
})
const { URL } = require('url'); // Use require instead of ES6 import
const a = new URL('https://example.com:8080');
console.log(a);
console.log(a.href);
const http = require("http");
const fs = require("fs");
const dotenv = require("dotenv");
// Load environment variables from the .env file
dotenv.config();
const port = process.env.PORT || 8082;
const server = http.createServer((req, one client request but need to communicate ares) => {
res.statusCode = 200;
if (req.url === "/") {
res.setHeader("Content-Type", "text/html");
res.end("<h1>Homepage: Mahima Dhakal</h1>");
} else if (req.url === "/about") {
res.end("<h1>About: Mahima Dhakal</h1>");
} else if (req.url === "/check") {
const data = fs.readFileSync("./templates/index.html");
res.end(data);
} else {
res.statusCode = 400;
res.end("<h1>Not Found</h1>");
}
});
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
const express = require('express');
const app = express();
const port = 5008;
const path = require('path');
dir_name="/home/mahima/Documents/Node/templates"
app.get('/', (req, res) => {
res.sendFile(path.join(dir_name, 'index.html'));one client request but need to communicate a
});
app.get('/about', (req, res) => {
res.send("about word");
});
app.listen(port, () => {
console.log(`Listening on port: ${port}`);
});
Even Javascript completed its promise but he couldnt