Skip to content

Instantly share code, notes, and snippets.

@tromgy
tromgy / arrow-this.js
Created February 22, 2020 13:03
using 'this' inside arrow function
const person = {
name: 'lewis',
otherNames: ['paul', 'fairweather'],
prop: function () {
this.otherNames.forEach(n => console.log(this.name + ' ' + n));
}
};
@tromgy
tromgy / StringMatchExt.swift
Created December 28, 2018 02:51
iOS UITextField extension for regex input validation
import Foundation
extension String {
func matches(_ expression: String) -> Bool {
if let range = range(of: expression, options: .regularExpression, range: nil, locale: nil) {
return range.lowerBound == startIndex && range.upperBound == endIndex
} else {
return false
}
}
@tromgy
tromgy / index.js
Last active September 14, 2021 02:00
Enable CORS with Node.js express
const express = require('express');
const app = express();
// Enable CORS
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); // USE actual URL for the webserver instead of * in production
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', '*'); // Limit the methods to actually used in produciton
next();
@tromgy
tromgy / https.ts
Created November 15, 2018 15:36
Set up HTTPS with Node.js express
// TypeScript
import * as express from 'express';
import * as fs from 'fs';
import * as https from 'https';
const port = process.env.MY_HTTPS_PORT || 443;
const certPath = process.env.MY_CERT_PATH || './'; // wherever the certificate is
const keyFile = certPath.concat('cert.key');