1) What is NPM?
- With npm, we can easily install, manage, and update packages (Libraries/ Modules) that you can use in your applications.
npm i express
fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error(err); return; } console.log(data); }); |
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { if (err) throw err; console.log('Saved!'); }); |
fs.open('mynewfile2.txt', 'w', function (err, file) { if (err) throw err; console.log('Saved!'); }); |
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) { if (err) throw err; console.log('Saved!'); }); |
fs.unlink('myfile.txt', function (err) { if (err) throw err; console.log('File deleted!'); }); |
const url = require('url'); const urlString = 'https://example.com:8080/path?query=value'; const parsedUrl = url.parse(urlString, true); // Pass true as the second argument to parse query parameters console.log(parsedUrl); |
const url = require('url'); const baseUrl = 'https://example.com/users'; const relativePath = '/profile'; const resolvedUrl = url.resolve(baseUrl, relativePath); console.log(resolvedUrl); // returns https://example.com/profile |
// Import the built-in 'http' module const http = require('http'); // Define the hostname and port number const hostname = '127.0.0.1'; const port = 3000; // Create an HTTP server const server = http.createServer((req, res) => { // Set the HTTP status code and content type in the response header res.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body res.end('Hello, world!\n'); }); // Start the server and listen on the specified port and hostname server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); |
// Import the built-in 'http' module const http = require('http'); // Define the options for the HTTP request const options = { hostname: 'jsonplaceholder.typicode.com', port: 80, path: '/posts', method: 'GET' }; // Create the HTTP request const req = http.request(options, (res) => { // Initialize a variable to store the response data let data = ''; // Concatenate chunks of data as they are received res.on('data', (chunk) => { data += chunk; }); // When the response is complete, log the data to the console res.on('end', () => { console.log(data); }); }); // Handle errors that occur during the request req.on('error', (error) => { console.error('An error occurred:', error); }); // End the request req.end(); |
function greetWithPersonName(name) { console.log ( `Hello ${name}! `); //Here we have used the tilde operator } module.exports = greetWithPersonName; |
const fs = require ('fs'); fs.readFile ('text.txt, 'utf8', (err, data) => { if (err) { console.error (err); return; } console.log (data); }); |
// Import the built-in 'http' module
const http = require('http'); // Create an HTTP server that listens on port 5000 const server = http.createServer((req, res) => { // Set the response HTTP header with HTTP status and content type res.writeHead(200, {'Content-Type': 'text/plain'}); // Write the response body ("Hello, world!") res.end('Hello World!!'); }); // Start the server and listen on port 5000 server.listen(5000, () => { console.log('Server is now running at http://localhost:5000/'); }); |
// Factorial program using function
#include <stdio.h>
// Function prototype
long long calculateFactorial(int n);
int main() {
int number;
// Get input from the user
printf("Enter a non-negative integer: ");
scanf("%d", &number);
// Check if the input is non-negative
if (number < 0) {
printf("Please enter a non-negative integer.\n");
return 1; // Return with an error code
}
// Calculate and display the factorial using a loop
long long factorial = calculateFactorial(number);
printf("Factorial of %d = %lld\n", number, factorial);
return 0;
}
// Function to calculate factorial using a loop
long long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
long long result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
}
#include <stdio.h>
// Function prototypes
int getInput();
int addNumbers(int num1, int num2);
void displayResult(int result);
int main() {
// Get input from the user
int number1 = getInput();
int number2 = getInput();
// Perform addition
int sum = addNumbers(number1, number2);
// Display the result
displayResult(sum);
return 0;
}
// Function to get input from the user
int getInput() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return num;
}
// Function to add two numbers
int addNumbers(int num1, int num2) {
return num1 + num2;
}
// Function to display the result
void displayResult(int result) {
printf("The sum is: %d\n", result);
}
# Node js program using if else if statement
var a = 'route 1';
if (a === 'route 1'){
console.log ('Route is 1');
}else if ( a === 'route 2') {
console.log ('Route is 2');
}else {
console.log ( 'No Route');
}