DEV Community

Cover image for Journey To FullStack, Express
JerryMcDonald
JerryMcDonald

Posted on • Updated on

Journey To FullStack, Express

Here we are, training to become a full stack developer and trying to piece together and understand the cogs in the machine that is web and application development. Many sprouting developers begin their journey into development by learning some of the most building blocks of many applications. In my case, it was javascript, CSS, and HTML. These topics were pretty fun and straight forward. Given a code problem, I hack my way through to the solution. Always know where the gaps in my knowledge were and understanding the step I would have to take to fill them. CSS and HTML are also similar in that you have a specific goal in mind, and finding the solution is as easy as researching a particular language. Those were simple times, but eventually, if you want to earn your stripes and say you are a full stack developer, you will have to learn more. Here I will talk about one way to handle back-end requests. Express.js

If you want to build web applications, you will need web frameworks to support your development. One of the most popular frameworks for developing these server-side applications is Node.js. Node.js can take your javascript code and compile it much faster than machine code. Node.js also uses event-driven programming, which means that your code's flow is determined by whichever event is taking place. Node uses the input/output model, so while working with node.js, you will be dealing with the incoming request and the outgoing response.

Unfortunately, while working with Node.js, you can end up writing quite a good deal of code even if you are trying to serve up static images or links. One of the downfalls of code, no matter how good of a programmer you are, is the possibility of introducing more errors into your application. But there is a Node.js web application framework that can significantly reduce the code needed to handle and respond to incoming requests. It is called Express. Express is a package you can download to help you establish a connection to your server and write paths that can handle incoming messages. It will significantly reduce the code you will need to write on the back end. Assuming you are already familiar with Node.js, let us look at how we can access Express and some of the primary request handling functions. You will likely need the below functionality in every application you create.

  • Let us begin by getting Express installed from NPM. You can type this in your terminal while inside your project folder.

Alt Text

  • Inside of your server.js or your app.js file, you will need to require Express.
  • We can also use 'app' to reference our new instance of Express.
    Alt Text

  • Now, we can set up and listen to a specific port. In the code below, PORT will be assigned by our code to our process.env.PORT if it exists, or it will go to the default port we prefer.
    Alt Text

The application will execute the commands below (inside of app.use()) before each request handling. Remember that the order you specify these commands is essential because of javascript synchronous (top to bottom) nature.

  • The commands below are important in just about any application you are building. These will handle data parsing for JSON and URL-encoded requests Alt Text

Now you will have to handle REST requests. An easy example would be paths like GET and POST. Both routes will require a callback function that will allow you to handle the request, and if need be, the response.

  • In the below example at the "path" of your choice. The function will take in a GET request and respond with a simple string.
    Alt Text

  • In the below example, we are receiving a POST request at a specified path. Then we are assigning the body of that request to a variable. Then we can send our request to our database, which we can design to handle this type of request, and send us back a response.
    Alt Text
    You may now have a basic understanding of Express.js and how it can streamline node.js. When you are trying to build out full-stack applications, you want to use the best frameworks and libraries out there. Express can help with your application building and streamline your server request and response handling. There are still many topics to learn about front end and back end web development. But, the best way to learn is to start creating! Good luck in your journey to becoming a full stack developer.

Resources:

  1. Expressjs Wiki

  2. NPM Express quick start gulde

  3. MDN Express/Node introduction

Top comments (0)