Skip to content

Instantly share code, notes, and snippets.

@jmcdo29
jmcdo29 / Explaination.md
Created July 17, 2019 01:02
tsconfig-paths bootstrap file to make registering paths work without problem

I've always had trouble using the given node -r tsconfig-paths/register path/to/main function from the command line, my paths never import correctly and I always seem to have an error when running after a build. After doing a bunch of digging around through packages and StackOverflow Q&A's I finally found a way to use tsconfig-paths programatically.

The tsconfig-bootstrap.js file makes use of tsconfig-pathsto register the paths from the compiler, and more than likely does exactly what tsconfig-paths/register is supposed to do. After requiring your tsconfig file you can access your baseUrl and paths via the JSON scheme of the config file to 100% programatically set your path imports to be correct, so that is anything changes in your config file this function doesn't need any chagning!

After you have the function set up, jsut add the following segment to your npm start command (whatever you have it called) and watch as your server fires up seamlessly:

# normal start without bootstrap
node
@joshnuss
joshnuss / app.js
Last active March 4, 2024 00:01
Express.js role-based permissions middleware
// the main app file
import express from "express";
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db)
import authenticate from "./authentication"; // middleware for doing authentication
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request
const app = express(),
api = express.Router();
// first middleware will setup db connection