Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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-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')})