chiwormjava
v2.0.6
Published
A simple CLI tool to **view and copy Java data structure code** (like LinkedList, Stack, etc.) directly in your browser.
Readme
🚀 Java Code Watch
A simple CLI tool to view and copy Java data structure code (like LinkedList, Stack, etc.) directly in your browser.
📦 Installation
Option 1: Use without installing (recommended)
npx javacodewatchOption 2: Install locally
npm install chiwormjavaRun:
npx javacodewatchOption 3: Install globally (optional)
npm install -g chiwormjavaRun:
javacodewatchconst http = require('http') const fs = require('fs')
const server = http.createServer();
server.on('request', (req, res) => { const rstream = fs.createReadStream('./product.json',"utf-8"); rstream.pipe(res); })
server.on("request", (req, res) => {
const rstream = fs.createReadStream("./product.json", "utf-8");
rstream.on("data", (chunkData) => {
res.write(chunkData);
});
rstream.on("end", () => {
res.end(); // ✅ correct: just end response
});
rstream.on("error", (err) => {
console.log(err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("File not found");
});});
server.listen(3000, "127.0.0.1", () => { console.log("Server is running at http://127.0.0.1:3000"); });
import readline from "readline";
const r1 = readline.createInterface({ input: process.stdin, output: process.stdout })
// Armstrong let n; r1.question('Enter a num : ', (num) => { n = parseInt(num) let arm = 0; let og = n;
let len = num.length;
while(n > 0){
let dig = n % 10;
arm += Math.pow(dig,len);
n = Math.floor(n / 10);
}
if(og == arm)
console.log('Is Armstrong!!');
else
console.log('Is Not a Armstrong!!');})
// calculator let n1, n2; r1.question('Enter the num : ', (num1) => { n1 = parseInt(num1); r1.question('Enter the num : ', (num2) => { n2 = parseInt(num2);
let sum = n1 + n2;
let sub = n1 - n2;
let mul = n1 * n2;
let div = n1 / n2;
console.log('Addition : ', sum);
console.log('Subtraction : ', sub);
console.log('Multiplication : ', mul);
console.log('Division : ', div);
})})
// Even or ODD r1.question('Enter a num : ', (num) => { let n = num; if(n % 2 == 0) console.log('Even'); else console.log('Odd'); })
// palindrom r1.question('Enter a num : ', (num) => { let s = num.toString(); let revNum = s.split('').reverse().join(''); if(revNum == s) console.log('Is palindrome') else console.log('Not a palindrome') })
//Factorial of a Number r1.question('Enter a number: ', (num) => { let n = parseInt(num); let fact = 1;
for(let i = 1; i <= n; i++){
fact *= i;
}
console.log('Factorial:', fact);});
//Fibonacci Series r1.question('Enter number of terms: ', (num) => { let n = parseInt(num); let a = 0, b = 1;
console.log(a);
console.log(b);
for(let i = 2; i < n; i++){
let next = a + b;
console.log(next);
a = b;
b = next;
}});
//Prime Number Check r1.question('Enter a number: ', (num) => { let n = parseInt(num); let isPrime = true;
if(n <= 1) isPrime = false;
for(let i = 2; i <= Math.sqrt(n); i++){
if(n % i === 0){
isPrime = false;
break;
}
}
console.log(isPrime ? 'Prime' : 'Not Prime');});
//Sum of Digits r1.question('Enter a number: ', (num) => { let n = parseInt(num); let sum = 0;
while(n > 0){
sum += n % 10;
n = Math.floor(n / 10);
}
console.log('Sum of digits:', sum);});
//Reverse a Number r1.question('Enter a number: ', (num) => { let n = parseInt(num); let rev = 0;
while(n > 0){
let digit = n % 10;
rev = rev * 10 + digit;
n = Math.floor(n / 10);
}
console.log('Reversed number:', rev);});
//Largest of Three Numbers r1.question('Enter first number: ', (a) => { r1.question('Enter second number: ', (b) => { r1.question('Enter third number: ', (c) => {
let n1 = parseInt(a);
let n2 = parseInt(b);
let n3 = parseInt(c);
let max = Math.max(n1, n2, n3);
console.log('Largest:', max);
});
});});
//Largest of Three Numbers r1.question('Enter a number: ', (num) => { let n = parseInt(num);
for(let i = 1; i <= 10; i++){
console.log(`${n} x ${i} = ${n * i}`);
}});
//Count Digits r1.question('Enter a number: ', (num) => { let count = num.length; console.log('Total digits:', count); });
const http = require("http"); const fs = require("fs"); const path = require("path");
let counter = 0;
// Read HTML template const content = fs.readFileSync("./indexFinal.html", "utf-8");
const server = http.createServer((req, res) => {
counter++;
console.log(`Server hit ${counter} times`);
let url = req.url.toLowerCase();
// Serve CSS
if (url === "/style.css") {
const cssPath = path.join(__dirname, "style.css");
const style = fs.readFileSync(cssPath, "utf-8");
res.writeHead(200, { "Content-Type": "text/css" });
return res.end(style);
}
// Routing pages
let pageContent = "";
if (url === "/" || url === "/home") {
pageContent = "🏠 Welcome to Home Page";
}
else if (url === "/about") {
pageContent = "📖 About Us Page";
}
else if (url === "/contact") {
pageContent = "📞 Contact Page";
}
else if (url === "/support") {
pageContent = "🛠 Support Page";
}
else {
res.writeHead(404, { "Content-Type": "text/html" });
return res.end(content.replace("{{CONTENT}}", " 404 Page Not Found"));
}
// Common response
res.writeHead(200, { "Content-Type": "text/html" });
res.end(content.replace("{{CONTENT}}", pageContent));});
server.listen(3000, "127.0.0.1", () => { console.log("Server running at http://127.0.0.1:3000"); });
const http = require("http"); const fs = require("fs");
let counter = 0;
const content = fs.readFileSync("./Template/index.html", "utf-8");
const server = http.createServer((request, response) => {
counter++;
console.log(`Server started ${counter} times`);
let path = request.url.toLowerCase();
if (path === "/" || path === "/home") {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(content.replace("{{CONTENT}}", "You are in home page"));
}
else if (path === "/about") {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(content.replace("{{CONTENT}}", "You are in about us page"));
}
else if (path === "/contact") {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(content.replace("{{CONTENT}}", "You are in contact page"));
}
else if (path === "/support") {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(content.replace("{{CONTENT}}", "Support me aagya bhai"));
}
else if (path === "/product") {
fs.readFile("./data/products.json", "utf-8", (error, jsonData) => {
if (error) {
response.writeHead(500, { "Content-Type": "text/plain" });
return response.end("Error reading JSON file");
}
response.writeHead(200, { "Content-Type": "application/json" });
response.end(jsonData);
});
}
// 404
else {
response.writeHead(404, { "Content-Type": "text/html" });
response.end(content.replace("{{CONTENT}}", "Error : 404 NOT FOUND"));
}});
server.listen(3000, "127.0.0.1", () => { console.log("Server running at http://127.0.0.1:3000"); });
[ { "id": 1, "name": "Laptop", "price": 60000 }, { "id": 2, "name": "Phone", "price": 20000 }, { "id": 3, "name": "Tablet", "price": 30000 } ]
GET
const express = require('express'); const fs = require('fs');
const app = express();
// Helper function to read data function getProducts() { const data = fs.readFileSync('./product.json', 'utf-8'); return JSON.parse(data).products; }
// Home route app.get('/', (req, res) => { res.status(200).send('You are on home page'); });
// About route app.get('/about', (req, res) => { res.status(200).send('You are on about page'); });
// Get all products app.get('/product', (req, res) => { const products = getProducts();
res.status(200).json({
status: 'success',
results: products.length,
data: products
});});
// Get single product by ID app.get('/product/:id', (req, res) => { const products = getProducts(); const id = parseInt(req.params.id);
const product = products.find(el => el.id === id);
if (!product) {
return res.status(404).json({
status: 'fail',
message: 'Product not found'
});
}
res.status(200).json({
status: 'success',
data: product
});});
// Contact route app.get('/contact', (req, res) => { res.status(200).send('You are on contact page'); });
app.listen(3000, () => { console.log('Server running on port 3000'); });
POST
const express = require('express'); const fs = require('fs');
const app = express();
app.use(express.json());
function getProducts() { const data = fs.readFileSync('./product.json', 'utf-8'); return JSON.parse(data).products; }
function saveProducts(products) { fs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2)); }
app.post('/product', (req, res) => {
const products = getProducts();
const newProduct = req.body;
if (!newProduct.name || !newProduct.price) {
return res.status(400).json({
status: 'fail',
message: 'Name and price are required'
});
}
const newId = products.length > 0
? products[products.length - 1].id + 1
: 1;
newProduct.id = newId;
products.push(newProduct);
saveProducts(products);
res.status(201).json({
status: 'success',
message: 'Product added successfully',
data: newProduct
});});
app.listen(3000, () => { console.log('Server running on port 3000'); });
PATCH
const express = require('express'); const fs = require('fs');
const app = express();
app.use(express.json());
// Helper functions function getProducts() { const data = fs.readFileSync('./product.json', 'utf-8'); return JSON.parse(data).products; }
function saveProducts(products) { fs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2)); }
app.patch('/product/:id', (req, res) => {
const products = getProducts();
const id = parseInt(req.params.id);
const product = products.find(el => el.id === id);
if (!product) {
return res.status(404).json({
status: 'fail',
message: 'Product not found'
});
}
if (req.body.id) {
return res.status(400).json({
status: 'fail',
message: 'Cannot update product id'
});
}
Object.assign(product, req.body);
saveProducts(products);
res.status(200).json({
status: 'success',
message: 'Product updated successfully',
data: product
});});
app.listen(3000, () => { console.log('Server running on port 3000'); });
DELETE
const express = require('express'); const fs = require('fs');
const app = express();
app.use(express.json());
function getProducts() { const data = fs.readFileSync('./product.json', 'utf-8'); return JSON.parse(data).products; }
function saveProducts(products) { fs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2)); }
app.delete('/product/:id', (req, res) => {
const products = getProducts();
const id = parseInt(req.params.id);
const index = products.findIndex(el => el.id === id);
if (index === -1) {
return res.status(404).json({
status: 'fail',
message: 'Product not found'
});
}
const deletedProduct = products.splice(index, 1);
saveProducts(products);
res.status(200).json({
status: 'success',
message: 'Product deleted successfully',
data: deletedProduct[0]
});});
app.listen(3000, () => { console.log('Server running on port 3000'); });
PUT
const express = require('express'); const fs = require('fs');
const app = express();
app.use(express.json());
// Helper functions function getProducts() { const data = fs.readFileSync('./product.json', 'utf-8'); return JSON.parse(data).products; }
function saveProducts(products) { fs.writeFileSync('./product.json', JSON.stringify({ products }, null, 2)); }
// PUT: Replace product completely app.put('/product/:id', (req, res) => {
const products = getProducts();
const id = parseInt(req.params.id);
const index = products.findIndex(el => el.id === id);
if (index === -1) {
return res.status(404).json({
status: 'fail',
message: 'Product not found'
});
}
const newData = req.body;
if (!newData.name || !newData.price) {
return res.status(400).json({
status: 'fail',
message: 'Name and price are required'
});
}
newData.id = id;
products[index] = newData;
saveProducts(products);
res.status(200).json({
status: 'success',
message: 'Product replaced successfully',
data: newData
});});
app.listen(3000, () => { console.log('Server running on port 3000'); });
chalk
const chalk = require('chalk');
console.log(chalk.red.bold('Error!')); console.log(chalk.green.underline('Success!')); console.log(chalk.blue.italic('Info message')); console.log(chalk.white.bgBlue.bold(' Important Message ')); console.log(chalk.black.bgYellow(' Warning Block ')); console.log(chalk.bgRed.white.bold(' Critical Error ')); console.log(chalk.rgb(255, 136, 0)('Custom Orange Text')); console.log(chalk.hex('#00FFAA')('Hex Color Text')); console.log(chalk.bgHex('#FF5733').white('Styled Background')); console.log( chalk.blue('Hello ' + chalk.yellow.bold('World') + '!') ); console.log( chalk.red('H') + chalk.yellow('e') + chalk.green('l') + chalk.blue('l') + chalk.magenta('o') );
