Last active
September 6, 2023 04:13
-
-
Save dineshdeveloper1/e83aab04325ab05a68e473fd962a31ef to your computer and use it in GitHub Desktop.
node snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//index.html | |
<body> | |
<h1>Winter</h1> | |
<p>I love the snow!</p> | |
</body> | |
//read file | |
var http = require('http'); | |
var fs = require('fs'); | |
http.createServer(function (req, res) { | |
fs.readFile('index.html', function(err, data) { | |
if (err) { | |
res.writeHead(404, {'Content-Type': 'text/html'}); | |
return res.end("404 Not Found"); | |
} | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.write(data); | |
return res.end(); | |
}); | |
}).listen(8080); | |
// create file -------------------------------- | |
//appendFile | |
var fs = require('fs'); | |
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { | |
if (err) throw err; | |
console.log('Saved!'); | |
}); | |
//open file | |
var fs = require('fs'); | |
fs.open('mynewfile2.txt', 'w', function (err, file) { | |
if (err) throw err; | |
console.log('Saved!'); | |
}); | |
//write file | |
var fs = require('fs'); | |
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) { | |
if (err) throw err; | |
console.log('Saved!'); | |
}); | |
//update file ----------------------------------------------------- | |
//append file | |
var fs = require('fs'); | |
fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) { | |
if (err) throw err; | |
console.log('Updated!'); | |
}); | |
//write file | |
var fs = require('fs'); | |
fs.writeFile('mynewfile3.txt', 'This is my text', function (err) { | |
if (err) throw err; | |
console.log('Replaced!'); | |
}); | |
//delete file ------------------------------------------------------ | |
var fs = require('fs'); | |
fs.unlink('mynewfile2.txt', function (err) { | |
if (err) throw err; | |
console.log('File deleted!'); | |
}); | |
//rename file ---------------------------------------------------- | |
var fs = require('fs'); | |
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) { | |
if (err) throw err; | |
console.log('File Renamed!'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Core Modules =================================================================================== | |
const http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.write('Welcome to this page!'); | |
res.end(); | |
}).listen(3000); | |
// local Modules =================================================================================== | |
// calc.js | |
exports.add = function (x, y) { | |
return x + y; | |
}; | |
exports.sub = function (x, y) { | |
return x - y; | |
}; | |
// index.js | |
const calculator = require('./calc'); | |
let x = 50, y = 10; | |
console.log("Addition of 50 and 10 is " + calculator.add(x, y)); | |
console.log("Subtraction of 50 and 10 is " + calculator.sub(x, y)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//index.js | |
if(10 === '10' ){ | |
console.log('this is true') | |
}else { | |
console.log('this is false') | |
} | |
// run the command | |
node index.js | |
////////////////////////////////////////////////////////////////////// | |
let arr = [2, 4, 98, 32, 8, 43] | |
console.log(arr[0]) | |
///////////////////////////////////////////////////////////////////// | |
//app.js | |
module.export = { | |
x: 10, | |
y: 20, | |
z: function{ | |
return 98; | |
} | |
} | |
//index.js | |
const app = require('.app') | |
console.log(app.x) | |
console.log(app.a()) | |
///////////////////////////////////////////////////////////////////// | |
//filter - find values in array | |
let arr = [2, 4, 98, 32, 8, 43] | |
arr.filter((item) => { | |
console.log(item) | |
}) | |
let result = arr.filter((item) => { | |
return item === 32 | |
// return item > 22 | |
// return item <= 32 | |
}) | |
console.log(result) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = require('url'); | |
var adr = 'http://localhost:8080/default.htm?year=2017&month=february'; | |
var q = url.parse(adr, true); | |
console.log(q.host); //returns 'localhost:8080' | |
console.log(q.pathname); //returns '/default.htm' | |
console.log(q.search); //returns '?year=2017&month=february' | |
var qdata = q.query; //returns an object: { year: 2017, month: 'february' } | |
console.log(qdata.month); //returns 'february' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment