Express Middleware Error handlers

With express middleware there is a default error handling middleware that works okay for simple projects, but there will come a time now and then where it might be necessary to write custom error handling middleware functions and modules for major projects. Also one should be aware of how to make use of the next function in a middleware function when it comes to working with functions that have callbacks that might have an error object, or promises that can possibly trigger a catch block.

When writing an error handling middleware for express the process of doing so is more or less the same as writing any other middleware in express, only there are four arguments to be aware of rather than the usual three. There are many basic midleware functions where there are just two arguments one for the request object, and another for the response object. As one starts making more complex examples there is making use of a third next argument that is a function that can be called to continue to the next middelware function that may apply to the request. When it comes to errors there is a additional fourth argument as you might guess contains information about the kind of error that happened.

So then in this post I will be going over just a few quick, simple middleware functions that will take into account the possibility that and error might have happened. Nothing major, just the basics of error handing in express.

Read More

An express text editor example

I want to write a few posts on express examples that are actual full working application examples, rather than just simple hello world type examples. There is of course the typical todo app that is often the case, but I want to make a few more that go beyond that into other examples as well. As of late I have been transitioning from using windows to linux, and so far have been having a hard time finding a text editor that stacks up to notepad++ which I have grown accustomed to in windows. So why not make my own text editor on top of node.js, and express that I can take with me to any operating system that I can get node.js installed on? Sounds like a good idea to me compared to being dependent on a windows exclusive app, so I put together a quick basic expressjs powered text editor example.

Read More

Express examples from very basic to not so basic

This will be a post on express examples from very basic examples that are just use expressjs by itself, to more complex projects that use a fair number of npm packages, one or more front end frameworks, or a whole lot of vanilla js code. Learning express is something that will take a fair investment of time, but the more I play around with it the more I begin to like it actually. The framework takes a kind of minimalistic approach just like that of vuejs when it comes to front end frameworks, which I think pares well with express.

There is a lot to know before hand of course, but if you have been playing around with express for a while now and are not looking for some full express app examples of one sort or another on this site this is the right place to be.

Read More

Express status getting and setting a http status code

In express status codes can be both get and set with properties and methods in an express response object. There is the res.statusCode property than can be used to find out the current http status code, and the res.status method that can be used to set that code. In addition there is the res.sendStatus method that can be used to just set a status code and end the request without sending any data in the same way as the express end response method. So this will be a post on http status codes in express, getting it, setting it and some status code use examples.

Read More