Skip to content

Instantly share code, notes, and snippets.

@jsuryahyd
jsuryahyd / server-port-farwarding.js
Last active April 10, 2018 07:25
Port farwarding in nodejs
const http = require('http');
http.createServer((req,res)=>{
let route = req.url;
if(route == '/'){
res.end('root route.')
}else if(route == '/hello'){
res.end('hello')
}
}).listen(3000,()=>{console.log('listening on a domain')})
@jsuryahyd
jsuryahyd / sanitizeTemplateString.js
Last active June 26, 2018 13:22
Escapes HTML characters in a template literal string, to prevent XSS attacks.
// Escapes HTML characters in a template literal string, to prevent XSS.
// See https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content
function sanitizeHTML(strings) {
const entities = {'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'};
let result = strings[0];
for (let i = 1; i < arguments.length; i++) {
result += String(arguments[i]).replace(/[&<>'"]/g, (char) => {
return entities[char];
});
result += strings[i];
@jsuryahyd
jsuryahyd / server.js
Created April 19, 2018 11:51
express server run with both http and https
const express = require('express');
const path = require('path');
const util = require('util');
const fs = require('fs');
const morgan = require('morgan')
const https = require('https')
const app = express();
//static
app.use(express.static(path.join(__dirname,'./public')));
@jsuryahyd
jsuryahyd / db.js
Created April 21, 2018 13:45
async module for querying multiple database queries
const mysql = require("mysql");
const async = require('async')
const dbConfig = require("./tables.js").tables;
const db = mysql.createPool({
host: "localhost",
user: dbConfig.username,
password: dbConfig.password,
database: "click2magic",
multipleStatements:true
});
@jsuryahyd
jsuryahyd / selectText.js
Created October 24, 2018 14:04
Select text with javascript on the browser.
//dont remember, where got this from, but,
function selectText(id) {
var node = document.getElementById(id);
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
--connection status
show status like '%conn%';
--full process list
SHOW full processlist;
--global variables
show global variables like '%connect%';
--session variables
SHOW SESSION VARIABLES LIKE "%connect%";
@jsuryahyd
jsuryahyd / writeLargeFile.js
Last active June 15, 2019 11:30
write large text to file using nodejs
//writes a billion numbers to file. size: 9.3GB
let writeStream = fs.createWriteStream("./output");
writeStream.on('open',async ()=>{
while (k<=10**9){
let written = k%10 == 0 ? writeStream.write(k.toString()+"\n") : writeStream.write(k.toString()+" ");
/**
imp stuff..., without which out-of-memory error is thrown.
[https://stackoverflow.com/a/40221132/7314900] stream.write() method returns false when stream has large enough data. so,
*/
if(!written){
@jsuryahyd
jsuryahyd / README.md
Created July 9, 2019 11:27
react native project readme

Native code

android picker styling

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorControlNormal">#00000000</item>
        <item name="android:spinnerStyle">@style/SpinnerStyle</item>
@jsuryahyd
jsuryahyd / launch.json
Created August 14, 2019 10:15
typescript project vscode debugging inside .vscode folder
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Development",
@jsuryahyd
jsuryahyd / server.js
Last active November 27, 2019 06:45
node server for serving static files when using angular or react built in production mode
const http = require("http");
const fs = require("fs");
const pathToStaticFilesFolder = "./dist";//or whererver
http
.createServer((req, res) => {
console.log(req.url);
let fileStream;
//if no path or path doesnot contain a file extension