Skip to content

Instantly share code, notes, and snippets.

View MR-DS-20's full-sized avatar

MXDS20 MR-DS-20

View GitHub Profile
@MR-DS-20
MR-DS-20 / example.router.ts
Created June 28, 2021 10:36
An example of an express router
import express = require('express');
export const routerTemplate = express.Router();
//controllers
import { exampleController } from "../controllers/controllers.module";
// Set the common part of the path for the routes in this router
const base = '/example-path'
//Routes
@MR-DS-20
MR-DS-20 / example.controller.ts
Created June 28, 2021 10:26
Example of a controller as part of an Express API
import mongoose = require("mongoose");
import { BaseController } from "./base.controller";
import { Response, Request } from "express";
import { ExampleModel } from "../models/example.model";
import { ExampleDoc } from "../interfaces/example.interface";
export class ExampleController extends BaseController {
constructor() {
super(new ExampleModel());
}
@MR-DS-20
MR-DS-20 / Base_controller_express.ts
Last active June 28, 2021 15:01
Start of an aexample of a base class fro an express API controller
import { Response } from "express";
import mongoose = require("mongoose");
import { env } from "../environment/env";
import { IModel } from "../interfaces/IModel";
import { IPopulate } from "../interfaces/IPopulate";
import { BaseModel } from "../models/base.model";
/**
* Provides functions to be used with express routes. Serves common CRUD fuctionality.
*/
@MR-DS-20
MR-DS-20 / mongoos_model_schema.ts
Last active June 25, 2021 17:49
Example of how to create a mongoose schema and model in typescript
const NestedSchema = new Schema({
name: {type: String}
})
const ExampleSchema: Schema = new Schema({
title: { type: String },
date_created: { type: Number },
date_modified: { type: Number },
order: { type: Number, default: 0 },
hide: { type: Boolean, default: false },
@MR-DS-20
MR-DS-20 / folder-structure.txt
Created June 25, 2021 17:26
Folder structure for express template
|-controllers/
|---controllers.module.ts
|---base.controller.ts
|---example.controller.ts
|-environment/
|---dev.ts
|---env.ts
|---prod.ts
|-interfaces/
|-models/
@MR-DS-20
MR-DS-20 / server.ts
Last active June 25, 2021 15:58
Example of a express server file
import { env } from "./environment/env";
import { App } from "./application";
import { middleware } from "./middleware";
import { routerTemplate } from "./routes/template.router";
const port: number = env().port ?? 8080;
let dbConString;
try {
dbConString = env().db.uri(
@MR-DS-20
MR-DS-20 / env.ts
Last active June 25, 2021 15:34
Function that can perform file replcement for environment variables
export const env: () => IEnv = () => {
if (process.env.ENVIRONMENT === "dev") {
let env = require("./dev");
return env;
} else if (process.env.ENVIRONMENT === "production") {
let env = require("./prod");
return env;
} else {
console.log(
`Error. shell variable ENVIRONMENT not set. Acceptable values are 'dev' | 'production'`
@MR-DS-20
MR-DS-20 / Express_App.ts
Last active June 25, 2021 15:53
Partial example of a Typescript class the creates a NodeJS express app
/**
* Class that takes in all the variables needed to construct an express server
*/
import { env } from "./environment/env";
import { Application } from "express";
import express = require("express");
import mongoose = require("mongoose");
export class App {
@MR-DS-20
MR-DS-20 / sum each input line
Created March 17, 2021 17:13
Shell awk command to sum numbers from each line of stdin
awk '{s+=$1} END {printf "%.0f", s}'
@MR-DS-20
MR-DS-20 / git files by size
Created March 17, 2021 17:09
List all files in get repo by size
git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4