Uploading Files to Express

I’ve never uploaded a blob file to an express backend, or MongoDB before. All the times that I’ve learned backend development, I was mainly handling JSON data types. Which is why, when I tried sending a POST request to an express backend with a body that contains a File data type, this error occurred: 1 2 request entity too large PayloadTooLargeError: request entity too large After a quick googling, it turns out that there are a few things that I’ll need to know about “uploading” files to a DB through an express backend. ...

May 16, 2023 · 2 min · Lei

Notes on Error handling

The hallmark of an experienced developer is his/her ability to foresee the errors and handle them appropriately. Below are some notes regarding how error handling is done in different contexts. Let’s first take a look at the general structure of error handling. Structure of error handling Within a .then().catch() block For instance, within a route, you want to find the noteById, and then do something. 1 2 3 4 5 6 Note.findById(id) .then(note => note) .catch(error => { console.log(error) response.status(500).end() }) Often times, when you’re making a request to the database, it’s unclear whether your request will succeed. If there is an error, you’ll need a .catch() block to handle the error. The catch block receives a callback function with error as argument - an error handler function - and you’ll then define how to handle this error. ...

September 24, 2021 · 5 min · Lei

Steps for Implementing a More Robust Backend Development Architecture Part2

In part 1, we mainly looked at laying out the groundwork for the backend project, including setting up console logging (morgan), eslint, installing various dependencies, configuration for MongoDB connection, util files such as logger, creating the models I needed, coding the express app (app.js), and finally, populating the database with some initial data to work with. In this part, I’ll begin by documenting how I go about creating and testing the routes. Then, we’ll look at middleware and error handling. Finally, I’ll connect the frontend to the backend to make sure that all the data is being pulled correctly. ...

September 23, 2021 · 4 min · Lei

Steps for Implementing a More Robust Backend Development Architecture (Part 1)

You’ve arrived at the moment where your project is getting serious and you need to begin coding some serious backend. This guide will get you started. Goal of Backend Restructure Goal of the development project is to get the backend to look like this: Backend architecture: Index.js App.js Routers: Streams Router Trades Router User Router Login Router Models Trades Model Streams Model User Model Utils Middleware Logger config.js Tests setting up test environment Laying the groundworks Step 1: Take stock of current project dependencies and structure Examine package.json to see what dependencies it currently have. Examine the project folder structure and files to check where is the backend mostly stored. For a simple express server, it is possible that all the routes, middleware, models, are stored in index.js. ...

September 22, 2021 · 8 min · Lei

Refactoring Express Backend

I feel like there is a lot to take in for this part. Whilst in the last part, all of the backend logic was basically stored in index.js, now we’ve refactored each portion into their own separate modules. Indeed, I can see how this could be an overkill for smaller apps, but for more complex projects, it would certainly ‘make developing the app much easier’. Let’s take a look at how that is done, line by line. ...

September 10, 2021 · 6 min · Lei

Steps for Setting Up the Backend During Development

Alrighttttt I am about to embark on some backend development for my Streams app. This document acts as a kind of guide for future development processes, as the exact processes are rather rusty in my mind. I am starting out from completely no backend on my simple React app, with a couple of frontend components. I think the first thing I need to do is to set up an express server. ...

August 25, 2021 · 3 min · Lei

Express: Auto Server Refresh Feature

One problem with the simple Express server is that you have to restart the application every time there is a change to the source code to see the changes implemented. By installing nodemon, you have an “auto-refresh upon source code save” feature. Step 0: Go to Express project root. Step 1: Install nodemon npm install --save-dev nodemon Step 2: Add the following line: "dev": "nodemon index.js" to package.json 1 2 3 4 5 "scripts": { "start": "node index.js", "dev": "nodemon index.js", "test": "echo \"Error..."" }

August 16, 2021 · 1 min · Lei

Steps for creating an Express Server

It is really simple creating an express API server. All you have to do is basically requiring express, creating/importing the JSON data, setting up a route, setting up a listening port, and bam there is API access to your data. Steps for creating an Express API server: Step 1: Importing Express, assigning it to app variable 1 2 const express = require('express') const app = express() Step 2: Creating JSON data ...

August 16, 2021 · 1 min · Lei