SIGN IN

1. Express.js Lessons. Basics and Middleware. Part 1.

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter correct name
Please enter correct email
Looks good!

expeess_p1

Hey all! This and next articles will be devoted to various themes on development within the context of one app that we will consequently improve. This app is a web chat. Not a simple one, but the web chat that should demonstrate correct configuration, how to create common pages using Node.js and the Express framework, how to do templating, JSON service, authorization, chat, Socket, how to work with a database, to name just a few. The articles will contain materials created by using the framework version Express 3, the current version of Express 4. Out of date features of Express 3 are not used in the articles, so the only cardinal difference in Express 4 is that a number of libraries have been taken out of the framework – see Migrating from 3.x to 4.x. If you want to follow our lessons, we recommend:

npm i express@3

And your further transition to the Version 4 will be obvious.

express

So, let us start with Express technology, and we will get familiar with other aspects mentioned above while developing our chat.

So, an empty directory. To create our own website, we will use the Express system :

npm install i -g express@3.3.8

Express is a Node.js framework that is mostly used for creating websites and online services. Let us install it globally because we will need a specialized Express utility. It enables to quickly generate the website structure, its main files

 express –help

shows options. We will need:

express –s  –e

which means we will add both the support of sessions and the template engine --ejs to our new website. The way how it is included and works will be explored in the process. So, everything’s done.

Open it in WebStorm. You can use any other editor convenient for you. So, look what’s been generated. App.js is a main file, and we will analyze its details very soon.

 Package.json–is a main file of the app. Add the title of our project there.

To launch the project you need dependencies. So, we open the console again and enter:

 npm i 

Everything contained in рackage.json will be installed in node_modules. So, a new directory appeared. All the modules needed for work have also been installed. In order to launch it, let us create the launch configuration аpp.js; put a tick inside Single instance only for your convinient restart. Launch it. Go to Chrome:

http://localhost:3000/

This is a port, where our app ‘listens‘ to something, and it works.

Go back to the project now. There are a lot of things generated; to make it easier for us, let us only the vital things necessary for Express . These are:

– to connect Express,
– to create an app, which creates a function used for handling requests:

Further add the http-server:

Respectively, Express will handle all incoming requests. We will also need http modules.

Aрp.get gets a value from a special hidden property of the object арpwhich can be installed as follows (add to our app.js):

Get all the rest commented out, we do not need these things.

Now, if we launch all these things, we will get a server listening on port 3000 and doing nothing. But why? Because the еxpress function doesn’t handle requests by default. To make it handle them, you need to add a special handler called Middleware  in Express  terms . It looks just the same as an average function, accepts req and res, transfers all objects and can do certain things:

This function will respond in the same way on all requests. Launch it. Check. It works!

How does Middleware differ from an average handler on.request? It has the third parameter called  next. It serves to integrate Middleware into chains. For example, you’ve got one Middleware and do another Middleware below. First of them checks, whether it is

it calls

Otherwise, it delivers the control further:

We will continue soon, please stay tuned!  The lesson code can be found here.

keep-calm-there-s-more-to-come-14

The materials for this article have been borrowed from the following screencast.

We are looking forward to meeting you on our website soshace.com

 

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter correct name
Please enter correct email
Looks good!

Related articles

The Path of the Self-Taught Programmer: Avoiding Common Problems

In this article, we’ll explore how a self-taught programmer comes to be: which education opportunities they can utilize, which problems they may>>>

Programming

Introduction to GitHub Desktop: A GUI Enhancement to a CLI Approach

In this article, we'll explore various use cases of GitHub Desktop -- GUI software designed to ehance your git workflow. Does it hold up against CLI>>>

Programming
29.10.2019

Python Array Explained and Visualized

In this tutorial, we’ll delve into how Python arrays work, what their limitations are, and how you can use them to maximize their>>>

Programming

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy