chiwormjava 2.0.5 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/java.json +35 -27
- package/package.json +1 -1
- package/readme.md +655 -837
package/java.json
CHANGED
|
@@ -1,39 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"title": "
|
|
4
|
-
"code": "
|
|
2
|
+
"nodejs_stream_server": {
|
|
3
|
+
"title": "Node.js File Streaming using Streams",
|
|
4
|
+
"code": "const http = require('http');\\nconst fs = require('fs');\\n\\nconst server = http.createServer();\\n\\nserver.on('request', (req, res) => {\\n\\tconst rstream = fs.createReadStream('./product.json', 'utf-8');\\n\\trstream.pipe(res);\\n});\\n\\nserver.on('request', (req, res) => {\\n\\n\\tconst rstream = fs.createReadStream('./product.json', 'utf-8');\\n\\n\\trstream.on('data', (chunkData) => {\\n\\t\\tres.write(chunkData);\\n\\t});\\n\\n\\trstream.on('end', () => {\\n\\t\\tres.end();\\n\\t});\\n\\n\\trstream.on('error', (err) => {\\n\\t\\tconsole.log(err);\\n\\t\\tres.writeHead(500, { 'Content-Type': 'text/plain' });\\n\\t\\tres.end('File not found');\\n\\t});\\n\\n});\\n\\nserver.listen(3000, '127.0.0.1', () => {\\n\\tconsole.log('Server is running at http://127.0.0.1:3000');\\n});"
|
|
5
5
|
},
|
|
6
|
-
"
|
|
7
|
-
"title": "
|
|
8
|
-
"code": "
|
|
6
|
+
"nodejs_multiple_programs": {
|
|
7
|
+
"title": "Node.js Multiple Programs using Readline",
|
|
8
|
+
"code": "import readline from 'readline';\\n\\nconst r1 = readline.createInterface({\\n\\tinput: process.stdin,\\n\\toutput: process.stdout\\n});\\n\\n// Armstrong\\nr1.question('Enter a num : ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet arm = 0;\\n\\tlet og = n;\\n\\tlet len = num.length;\\n\\n\\twhile (n > 0) {\\n\\t\\tlet dig = n % 10;\\n\\t\\tarm += Math.pow(dig, len);\\n\\t\\tn = Math.floor(n / 10);\\n\\t}\\n\\n\\tif (og == arm)\\n\\t\\tconsole.log('Is Armstrong!!');\\n\\telse\\n\\t\\tconsole.log('Is Not a Armstrong!!');\\n});\\n\\n// Calculator\\nr1.question('Enter the num : ', (num1) => {\\n\\tlet n1 = parseInt(num1);\\n\\n\\tr1.question('Enter the num : ', (num2) => {\\n\\t\\tlet n2 = parseInt(num2);\\n\\n\\t\\tconsole.log('Addition : ', n1 + n2);\\n\\t\\tconsole.log('Subtraction : ', n1 - n2);\\n\\t\\tconsole.log('Multiplication : ', n1 * n2);\\n\\t\\tconsole.log('Division : ', n1 / n2);\\n\\t});\\n});\\n\\n// Even or Odd\\nr1.question('Enter a num : ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tconsole.log(n % 2 == 0 ? 'Even' : 'Odd');\\n});\\n\\n// Palindrome\\nr1.question('Enter a num : ', (num) => {\\n\\tlet s = num.toString();\\n\\tlet revNum = s.split('').reverse().join('');\\n\\n\\tconsole.log(revNum == s ? 'Is palindrome' : 'Not a palindrome');\\n});\\n\\n// Factorial\\nr1.question('Enter a number: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet fact = 1;\\n\\n\\tfor (let i = 1; i <= n; i++) {\\n\\t\\tfact *= i;\\n\\t}\\n\\n\\tconsole.log('Factorial:', fact);\\n});\\n\\n// Fibonacci\\nr1.question('Enter number of terms: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet a = 0, b = 1;\\n\\n\\tconsole.log(a);\\n\\tconsole.log(b);\\n\\n\\tfor (let i = 2; i < n; i++) {\\n\\t\\tlet next = a + b;\\n\\t\\tconsole.log(next);\\n\\t\\ta = b;\\n\\t\\tb = next;\\n\\t}\\n});\\n\\n// Prime Number\\nr1.question('Enter a number: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet isPrime = true;\\n\\n\\tif (n <= 1) isPrime = false;\\n\\n\\tfor (let i = 2; i <= Math.sqrt(n); i++) {\\n\\t\\tif (n % i === 0) {\\n\\t\\t\\tisPrime = false;\\n\\t\\t\\tbreak;\\n\\t\\t}\\n\\t}\\n\\n\\tconsole.log(isPrime ? 'Prime' : 'Not Prime');\\n});\\n\\n// Sum of Digits\\nr1.question('Enter a number: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet sum = 0;\\n\\n\\twhile (n > 0) {\\n\\t\\tsum += n % 10;\\n\\t\\tn = Math.floor(n / 10);\\n\\t}\\n\\n\\tconsole.log('Sum of digits:', sum);\\n});\\n\\n// Reverse Number\\nr1.question('Enter a number: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\tlet rev = 0;\\n\\n\\twhile (n > 0) {\\n\\t\\tlet digit = n % 10;\\n\\t\\trev = rev * 10 + digit;\\n\\t\\tn = Math.floor(n / 10);\\n\\t}\\n\\n\\tconsole.log('Reversed number:', rev);\\n});\\n\\n// Largest of Three Numbers\\nr1.question('Enter first number: ', (a) => {\\n\\tr1.question('Enter second number: ', (b) => {\\n\\t\\tr1.question('Enter third number: ', (c) => {\\n\\n\\t\\t\\tlet n1 = parseInt(a);\\n\\t\\t\\tlet n2 = parseInt(b);\\n\\t\\t\\tlet n3 = parseInt(c);\\n\\n\\t\\t\\tconsole.log('Largest:', Math.max(n1, n2, n3));\\n\\t\\t});\\n\\t});\\n});\\n\\n// Table\\nr1.question('Enter a number: ', (num) => {\\n\\tlet n = parseInt(num);\\n\\n\\tfor (let i = 1; i <= 10; i++) {\\n\\t\\tconsole.log(`${n} x ${i} = ${n * i}`);\\n\\t}\\n});\\n\\n// Count Digits\\nr1.question('Enter a number: ', (num) => {\\n\\tconsole.log('Total digits:', num.length);\\n});"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"title": "
|
|
12
|
-
"code": "
|
|
10
|
+
"node_html_template": {
|
|
11
|
+
"title": "Node.js Dynamic HTML Template",
|
|
12
|
+
"code": "<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n<head>\\n <meta charset=\\\"UTF-8\\\">\\n <title>My App</title>\\n\\n <style>\\n * {\\n margin: 0;\\n padding: 0;\\n box-sizing: border-box;\\n font-family: Arial, sans-serif;\\n }\\n\\n body {\\n background: #f5f7fa;\\n }\\n\\n .navbar {\\n display: flex;\\n justify-content: space-between;\\n padding: 15px 40px;\\n background: #333;\\n color: white;\\n }\\n\\n .navbar a {\\n color: white;\\n margin-left: 20px;\\n text-decoration: none;\\n }\\n\\n .hero {\\n text-align: center;\\n padding: 60px 20px;\\n }\\n\\n .hero h1 {\\n font-size: 36px;\\n }\\n\\n .hero p {\\n margin: 10px 0;\\n }\\n\\n .features {\\n display: flex;\\n justify-content: center;\\n gap: 20px;\\n padding: 40px;\\n }\\n\\n .card {\\n background: white;\\n padding: 20px;\\n width: 200px;\\n text-align: center;\\n border-radius: 8px;\\n }\\n\\n .dynamic {\\n text-align: center;\\n margin: 20px;\\n font-size: 20px;\\n color: #333;\\n }\\n\\n footer {\\n text-align: center;\\n padding: 15px;\\n background: #333;\\n color: white;\\n }\\n </style>\\n</head>\\n\\n<body>\\n\\n <nav class=\\\"navbar\\\">\\n <h2>MyApp</h2>\\n <div>\\n <a href=\\\"/home\\\">Home</a>\\n <a href=\\\"/about\\\">About</a>\\n <a href=\\\"/contact\\\">Contact</a>\\n <a href=\\\"/support\\\">Support</a>\\n </div>\\n </nav>\\n\\n <div class=\\\"dynamic\\\">\\n {{CONTENT}}\\n </div>\\n\\n <section class=\\\"hero\\\">\\n <h1>Simple Node Application</h1>\\n <p>This page is served using a basic Node.js server.</p>\\n </section>\\n\\n <section class=\\\"features\\\">\\n <div class=\\\"card\\\">\\n <h3>Fast</h3>\\n <p>Handles requests efficiently</p>\\n </div>\\n <div class=\\\"card\\\">\\n <h3>Simple</h3>\\n <p>Easy routing and structure</p>\\n </div>\\n <div class=\\\"card\\\">\\n <h3>Flexible</h3>\\n <p>Can be extended easily</p>\\n </div>\\n </section>\\n\\n <footer>\\n <p>Basic Node Server Example</p>\\n </footer>\\n\\n</body>\\n</html>"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
15
|
-
"title": "
|
|
16
|
-
"code": "
|
|
14
|
+
"nodejs_routing_server": {
|
|
15
|
+
"title": "Node.js Routing Server with Dynamic HTML Template",
|
|
16
|
+
"code": "const http = require('http');\\nconst fs = require('fs');\\nconst path = require('path');\\n\\nlet counter = 0;\\n\\nconst content = fs.readFileSync('./indexFinal.html', 'utf-8');\\n\\nconst server = http.createServer((req, res) => {\\n\\n\\tcounter++;\\n\\tconsole.log(`Server hit ${counter} times`);\\n\\n\\tlet url = req.url.toLowerCase();\\n\\n\\tif (url === '/style.css') {\\n\\t\\tconst cssPath = path.join(__dirname, 'style.css');\\n\\t\\tconst style = fs.readFileSync(cssPath, 'utf-8');\\n\\n\\t\\tres.writeHead(200, { 'Content-Type': 'text/css' });\\n\\t\\treturn res.end(style);\\n\\t}\\n\\n\\tlet pageContent = '';\\n\\n\\tif (url === '/' || url === '/home') {\\n\\t\\tpageContent = '🏠 Welcome to Home Page';\\n\\t}\\n\\telse if (url === '/about') {\\n\\t\\tpageContent = '📖 About Us Page';\\n\\t}\\n\\telse if (url === '/contact') {\\n\\t\\tpageContent = '📞 Contact Page';\\n\\t}\\n\\telse if (url === '/support') {\\n\\t\\tpageContent = '🛠 Support Page';\\n\\t}\\n\\telse {\\n\\t\\tres.writeHead(404, { 'Content-Type': 'text/html' });\\n\\t\\treturn res.end(content.replace('{{CONTENT}}', '❌ 404 Page Not Found'));\\n\\t}\\n\\n\\tres.writeHead(200, { 'Content-Type': 'text/html' });\\n\\tres.end(content.replace('{{CONTENT}}', pageContent));\\n\\n});\\n\\nserver.listen(3000, '127.0.0.1', () => {\\n\\tconsole.log('Server running at http://127.0.0.1:3000');\\n});"
|
|
17
17
|
},
|
|
18
|
-
"
|
|
19
|
-
"title": "
|
|
20
|
-
"code": "
|
|
18
|
+
"nodejs_routing_with_api": {
|
|
19
|
+
"title": "Node.js Server with Routing and Product API",
|
|
20
|
+
"code": "const http = require('http');\\nconst fs = require('fs');\\n\\nlet counter = 0;\\n\\nconst content = fs.readFileSync('./Template/index.html', 'utf-8');\\n\\nconst server = http.createServer((request, response) => {\\n\\n\\tcounter++;\\n\\tconsole.log(`Server started ${counter} times`);\\n\\n\\tlet path = request.url.toLowerCase();\\n\\n\\tif (path === '/' || path === '/home') {\\n\\t\\tresponse.writeHead(200, { 'Content-Type': 'text/html' });\\n\\t\\tresponse.end(content.replace('{{CONTENT}}', 'You are in home page'));\\n\\t}\\n\\n\\telse if (path === '/about') {\\n\\t\\tresponse.writeHead(200, { 'Content-Type': 'text/html' });\\n\\t\\tresponse.end(content.replace('{{CONTENT}}', 'You are in about us page'));\\n\\t}\\n\\n\\telse if (path === '/contact') {\\n\\t\\tresponse.writeHead(200, { 'Content-Type': 'text/html' });\\n\\t\\tresponse.end(content.replace('{{CONTENT}}', 'You are in contact page'));\\n\\t}\\n\\n\\telse if (path === '/support') {\\n\\t\\tresponse.writeHead(200, { 'Content-Type': 'text/html' });\\n\\t\\tresponse.end(content.replace('{{CONTENT}}', 'Support me aagya bhai'));\\n\\t}\\n\\n\\telse if (path === '/product') {\\n\\n\\t\\tfs.readFile('./data/products.json', 'utf-8', (error, jsonData) => {\\n\\n\\t\\t\\tif (error) {\\n\\t\\t\\t\\tresponse.writeHead(500, { 'Content-Type': 'text/plain' });\\n\\t\\t\\t\\treturn response.end('Error reading JSON file');\\n\\t\\t\\t}\\n\\n\\t\\t\\tresponse.writeHead(200, { 'Content-Type': 'application/json' });\\n\\t\\t\\tresponse.end(jsonData);\\n\\t\\t});\\n\\t}\\n\\n\\telse {\\n\\t\\tresponse.writeHead(404, { 'Content-Type': 'text/html' });\\n\\t\\tresponse.end(content.replace('{{CONTENT}}', 'Error : 404 NOT FOUND'));\\n\\t}\\n\\n});\\n\\nserver.listen(3000, '127.0.0.1', () => {\\n\\tconsole.log('Server running at http://127.0.0.1:3000');\\n});"
|
|
21
21
|
},
|
|
22
|
-
"
|
|
23
|
-
"title": "
|
|
24
|
-
"code": "
|
|
22
|
+
"express_get_product_api": {
|
|
23
|
+
"title": "Express Server with Product API",
|
|
24
|
+
"code": "const express = require('express');\\nconst fs = require('fs');\\n\\nconst app = express();\\n\\nfunction getProducts() {\\n\\tconst data = fs.readFileSync('./product.json', 'utf-8');\\n\\treturn JSON.parse(data).products;\\n}\\n\\napp.get('/', (req, res) => {\\n\\tres.status(200).send('You are on home page');\\n});\\n\\napp.get('/about', (req, res) => {\\n\\tres.status(200).send('You are on about page');\\n});\\n\\napp.get('/product', (req, res) => {\\n\\tconst products = getProducts();\\n\\n\\tres.status(200).json({\\n\\t\\tstatus: 'success',\\n\\t\\tresults: products.length,\\n\\t\\tdata: products\\n\\t});\\n});\\n\\napp.get('/product/:id', (req, res) => {\\n\\tconst products = getProducts();\\n\\tconst id = parseInt(req.params.id);\\n\\n\\tconst product = products.find(el => el.id === id);\\n\\n\\tif (!product) {\\n\\t\\treturn res.status(404).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Product not found'\\n\\t\\t});\\n\\t}\\n\\n\\tres.status(200).json({\\n\\t\\tstatus: 'success',\\n\\t\\tdata: product\\n\\t});\\n});\\n\\napp.get('/contact', (req, res) => {\\n\\tres.status(200).send('You are on contact page');\\n});\\n\\napp.listen(3000, () => {\\n\\tconsole.log('Server running on port 3000');\\n});"
|
|
25
25
|
},
|
|
26
|
-
"
|
|
27
|
-
"title": "
|
|
28
|
-
"code": "
|
|
26
|
+
"express_post_product_api": {
|
|
27
|
+
"title": "Express API to Add Product (POST Request)",
|
|
28
|
+
"code": "const express = require('express');\\nconst fs = require('fs');\\n\\nconst app = express();\\n\\napp.use(express.json());\\n\\nfunction getProducts() {\\n\\tconst data = fs.readFileSync('./product.json', 'utf-8');\\n\\treturn JSON.parse(data).products;\\n}\\n\\nfunction saveProducts(products) {\\n\\tfs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2));\\n}\\n\\napp.post('/product', (req, res) => {\\n\\n\\tconst products = getProducts();\\n\\tconst newProduct = req.body;\\n\\n\\tif (!newProduct.name || !newProduct.price) {\\n\\t\\treturn res.status(400).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Name and price are required'\\n\\t\\t});\\n\\t}\\n\\n\\tconst newId = products.length > 0\\n\\t\\t? products[products.length - 1].id + 1\\n\\t\\t: 1;\\n\\n\\tnewProduct.id = newId;\\n\\n\\tproducts.push(newProduct);\\n\\n\\tsaveProducts(products);\\n\\n\\tres.status(201).json({\\n\\t\\tstatus: 'success',\\n\\t\\tmessage: 'Product added successfully',\\n\\t\\tdata: newProduct\\n\\t});\\n});\\n\\napp.listen(3000, () => {\\n\\tconsole.log('Server running on port 3000');\\n});"
|
|
29
29
|
},
|
|
30
|
-
"
|
|
31
|
-
"title": "
|
|
32
|
-
"code": "
|
|
30
|
+
"express_patch_product_api": {
|
|
31
|
+
"title": "Express API to Update Product (PATCH Request)",
|
|
32
|
+
"code": "const express = require('express');\\nconst fs = require('fs');\\n\\nconst app = express();\\n\\napp.use(express.json());\\n\\nfunction getProducts() {\\n\\tconst data = fs.readFileSync('./product.json', 'utf-8');\\n\\treturn JSON.parse(data).products;\\n}\\n\\nfunction saveProducts(products) {\\n\\tfs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2));\\n}\\n\\napp.patch('/product/:id', (req, res) => {\\n\\n\\tconst products = getProducts();\\n\\tconst id = parseInt(req.params.id);\\n\\n\\tconst product = products.find(el => el.id === id);\\n\\n\\tif (!product) {\\n\\t\\treturn res.status(404).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Product not found'\\n\\t\\t});\\n\\t}\\n\\n\\tif (req.body.id) {\\n\\t\\treturn res.status(400).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Cannot update product id'\\n\\t\\t});\\n\\t}\\n\\n\\tObject.assign(product, req.body);\\n\\n\\tsaveProducts(products);\\n\\n\\tres.status(200).json({\\n\\t\\tstatus: 'success',\\n\\t\\tmessage: 'Product updated successfully',\\n\\t\\tdata: product\\n\\t});\\n});\\n\\napp.listen(3000, () => {\\n\\tconsole.log('Server running on port 3000');\\n});"
|
|
33
33
|
},
|
|
34
|
-
"
|
|
35
|
-
"title": "
|
|
36
|
-
"code": "
|
|
34
|
+
"express_delete_product_api": {
|
|
35
|
+
"title": "Express API to Delete Product (DELETE Request)",
|
|
36
|
+
"code": "const express = require('express');\\nconst fs = require('fs');\\n\\nconst app = express();\\n\\napp.use(express.json());\\n\\nfunction getProducts() {\\n\\tconst data = fs.readFileSync('./product.json', 'utf-8');\\n\\treturn JSON.parse(data).products;\\n}\\n\\nfunction saveProducts(products) {\\n\\tfs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2));\\n}\\n\\napp.delete('/product/:id', (req, res) => {\\n\\n\\tconst products = getProducts();\\n\\tconst id = parseInt(req.params.id);\\n\\n\\tconst index = products.findIndex(el => el.id === id);\\n\\n\\tif (index === -1) {\\n\\t\\treturn res.status(404).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Product not found'\\n\\t\\t});\\n\\t}\\n\\n\\tconst deletedProduct = products.splice(index, 1);\\n\\n\\tsaveProducts(products);\\n\\n\\tres.status(200).json({\\n\\t\\tstatus: 'success',\\n\\t\\tmessage: 'Product deleted successfully',\\n\\t\\tdata: deletedProduct[0]\\n\\t});\\n});\\n\\napp.listen(3000, () => {\\n\\tconsole.log('Server running on port 3000');\\n});"
|
|
37
|
+
},
|
|
38
|
+
"express_put_product_api": {
|
|
39
|
+
"title": "Express API to Replace Product (PUT Request)",
|
|
40
|
+
"code": "const express = require('express');\\nconst fs = require('fs');\\n\\nconst app = express();\\n\\napp.use(express.json());\\n\\nfunction getProducts() {\\n\\tconst data = fs.readFileSync('./product.json', 'utf-8');\\n\\treturn JSON.parse(data).products;\\n}\\n\\nfunction saveProducts(products) {\\n\\tfs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2));\\n}\\n\\napp.put('/product/:id', (req, res) => {\\n\\n\\tconst products = getProducts();\\n\\tconst id = parseInt(req.params.id);\\n\\n\\tconst index = products.findIndex(el => el.id === id);\\n\\n\\tif (index === -1) {\\n\\t\\treturn res.status(404).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Product not found'\\n\\t\\t});\\n\\t}\\n\\n\\tconst newData = req.body;\\n\\n\\tif (!newData.name || !newData.price) {\\n\\t\\treturn res.status(400).json({\\n\\t\\t\\tstatus: 'fail',\\n\\t\\t\\tmessage: 'Name and price are required'\\n\\t\\t});\\n\\t}\\n\\n\\tnewData.id = id;\\n\\n\\tproducts[index] = newData;\\n\\n\\tsaveProducts(products);\\n\\n\\tres.status(200).json({\\n\\t\\tstatus: 'success',\\n\\t\\tmessage: 'Product replaced successfully',\\n\\t\\tdata: newData\\n\\t});\\n});\\n\\napp.listen(3000, () => {\\n\\tconsole.log('Server running on port 3000');\\n});"
|
|
41
|
+
},
|
|
42
|
+
"nodejs_chalk_styling": {
|
|
43
|
+
"title": "Node.js Console Styling using Chalk",
|
|
44
|
+
"code": "const chalk = require('chalk');\\n\\nconsole.log(chalk.red.bold('Error!'));\\nconsole.log(chalk.green.underline('Success!'));\\nconsole.log(chalk.blue.italic('Info message'));\\nconsole.log(chalk.white.bgBlue.bold(' Important Message '));\\nconsole.log(chalk.black.bgYellow(' Warning Block '));\\nconsole.log(chalk.bgRed.white.bold(' Critical Error '));\\nconsole.log(chalk.rgb(255, 136, 0)('Custom Orange Text'));\\nconsole.log(chalk.hex('#00FFAA')('Hex Color Text'));\\nconsole.log(chalk.bgHex('#FF5733').white('Styled Background'));\\n\\nconsole.log(\\n\\tchalk.blue('Hello ' + chalk.yellow.bold('World') + '!')\\n);\\n\\nconsole.log(\\n\\tchalk.red('H') +\\n\\tchalk.yellow('e') +\\n\\tchalk.green('l') +\\n\\tchalk.blue('l') +\\n\\tchalk.magenta('o')\\n);"
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
47
|
|