Skip to content

Instantly share code, notes, and snippets.

View zcaceres's full-sized avatar
👨‍💻
programming

Zach Caceres zcaceres

👨‍💻
programming
View GitHub Profile
@zcaceres
zcaceres / bootcamp-mastery-danger-of-conceptual-understanding.md
Created May 24, 2017 21:57
The Danger of So-Called 'Conceptual Understanding'

I often hear people say they're focusing on 'conceptual understanding' while they study. There's some superficial truth to this. You don't want to know a jumble of random commands or syntax and have no idea how it all hangs together.

But focusing on 'conceptual understanding' is deceptive and dangerous. Here's why.

First, 'conceptual understanding' is often used as an excuse to avoid the hard but high-value work of deliberate practice.

Good practice is hard because it strains you. Anyone who has spent three hours (or three days) working through a single Codewars problem knows how this feels! The earlier you are in your learning curve, the harder this is.

It's too easy to say "Screw that! I'll watch a video to focus on my conceptual understanding instead." This leads to a plague of YouTube tutorials and articles. While reading about programming might feel like learning, often it's just consumption – a little bit better than browsing the news or Facebook.

describe('Review', () => {
  let testReview = {}
  before('Await database sync', () => db.didSync)
  afterEach('Clear the tables', () => db.truncate({ cascade: true }))
  beforeEach(function() {
    testReview = {
      rating: 4.5,
      comment: 'A decent magnet!'
 }
...

  describe('association checks', () => {
  it('sets userId', () => {
    return Review.create(testReview) // object defined elsewhere
      .then(review => {
        return review.setUser(1)
      })
 .then(review => {
@zcaceres
zcaceres / mysterious-errors-sequelize-base-error.md
Last active January 29, 2023 22:33
Mysterious Errors – SequelizeBaseError: Insert or Update on Table Violates Foreign Key Constraints

Sequelize is a great library, but its documentation leaves a lot to be desired.

Recently, while writing unit tests for an e-commerce site project, I came across this cryptic error:

SequelizeBaseError: insert or update on table "reviews" violates foreign key constraint "reviews_user_id_fkey"

From the error, it's not too clear what has gone wrong.

The model I was testing had two associations, using Sequelize's belongsTo method.

Recently while working with Webpack, I saw this error.

WARNING in ./app/components/students.js
There is another module with an equal name when case is ignored.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Rename module if multiple modules are expected or use equal casing if one module is expected.

WARNING in ./app/components/Students.js
There is another module with an equal name when case is ignored.

Middleware

Middleware refers to modules that are chained 'between' when your Express app is instantiated and when your server is launched with app.listen(myPortNumber).

We'll stick to two easy examples that illustrate the usefulness of middleware: logging and static resources.

Registering and Using Middleware

Using middleware is simple. Use Node.js to require the package that you'd like to use. Then add the middleware to your app by calling it with .use(). The order that you call your middleware may matter, so look in the docs for your middleware to make sure it's well-positioned.

Here's how we would deploy the logging middleware Morgan.

Simplify Your Life with Express Routers

So far we've handled our requests and responses with ease. But we still haven't seen how Express solves the annoying problem of routing the many resources and pages that a modern website may have.

Routing to Sub-Resources

Let's imagine that our website has a sub-page dedicated to a collection of kittens at '/kittens'.

Easy, right? As we saw before, we could just add a route to our app like this:

      app.get('/kittens', function(request, response, next) {

Routing in Express.js

Now that our app is set up and configured, let's see how Express.js simplifies our routing.

Our First Route

Here's what our app currently looks like.

        const express = require('express'); // makes Express available in your app.
        const app = express();  // Creates an instance of Express, which allows us to begin routing.

Installing Express.js

To install Express.js, you'll first initialize an npm project in a directory of your choice. Then, create a file where you'll build your app. I'm calling mine app.js.

Now, run npm install --save express from your favorite command line interface. This will install Express along with its many dependencies.

You'll also need an up-to-date version of Node.js, since Express leverages Node's HTTP to work its magic.

Once Express.js is installed, we're ready to start building our app.

Require and Instantiate An Express App

Express.js is a Javascript library that handles web routing and HTTP requests for web applications.

Express builds on the native HTTP library in Node.js to allow for a simple, object-oriented approach to route your web application.

The creators of Express.js describe it as a 'minimalist framework', meaning that Express.js handles a few core tasks well, but does not include many nice-to-have features. Instead, you can enhance your Express.js application with middleware downloaded from npm or that you build yourself.

This series has four parts.

First, we'll look at HTTP routing, the problem that Express.js tries to solve.