Express.js Lessons. Logger, Configuration, Templating with EJS. Part 2.

express_2_2

Favicon is the connect  of Middleware that checks whether the url has a view of favicon.ico; if the answer is ‘yes’, it reads favicon  and outputs, otherwise it transfers control further. The logger outputs a record what kind of a request we’ve received. For example, if we launch the app now, the logger will output something, when we follow:

http://localhost:3000/

That is a standard  Httplog requests, while dev is a logging format. There are also other formats – for example,  default (let us add the respective record in app.js):

Various formats can be found in the connect supporting materials. Here you will find different variants of built-in formats. There is one more amazing setting for adding a record into log. It often writes into log upon a request ending. But we can indicate immediate: true, and it will write into log in the very beginning.

But what is the difference, if we talk about our code? If we’ve got the option immediatea logger will write into logand further, through next, transfer control over the next  MiddlewareIt is the most likely control flow. But we’ve got a different thing by default – the logger rewrites res.end to its function that writes into log upon being called. So, it turns out, if we log this way, without the flag  immediatethen if log doesn’t contain anything, it doesn’t mean there was no request.

Keep that thing in mind. It may happen that the request really existed, but hung up because NODE didn’t handle it. Respectively, everything will go to log only upon the request end.

BodyParser deals with reading the forms sent via the post method, reads JSON data sent using this method. It means, the request body gets parsed. The data transferred through post, as well as similar methods get read using flows. This is an asynchronous action. BodyParser deals with all these things, absolutely reads post. And if it’s  JSON, itparse it, and the data becomes accessible in req.body:

Once it has completely read the postthis Middleware transfers control further through next.

CookieParser parses as well, but cookies instead of the body. So, there may be headers like these: req.headers. It divides them and makes the respective properties of the object cookies:

Here we can specify an optional key the cookies will be signed with. Right now we don’t need this record and will talk about it later.

Router allows us to talk seamlessly about what requests will be there and how they will be handled. For instance, let us add the following code:

Respectively, instead of get there may be post, putdel, etc. Moreover, this Middleware contains a number of extra options (you may transfer parameters, etc.).

The last Middleware is static. Generally, static gets displayed by other servers, not Node.js, but it can do it, too. So, if no Middleware handles the request here, the control gets delivered to Middleware static.

It checks, whether the  public directory has got the respective file. Let us change the names of public directories a little bit(stylesheets-> css,  javascript->js). Launch it, everything works!

App.js looks like that, check it out:

Eventually, instead of such a simple message, let us output a common HTML page. For that reason, I will create a new file in the template directory and call it index.ejs. The file extension is  ejssince a ejs-related template is usually created this way. The template itself is an HTML, and you can use special extra dividers in order to insert the code or variables, for example:

We can find more detailed information in our technical materials. If we want some special example, the code may be:

<% if (user) { means inserting the JavaScript code, which means the operator if will be executed, while %= means adding the variable values.  <%-body%> also means inserting of a variable. But if there is some kind of an unsafe text in the  body  (for example, <script>), in this case – <%-body%> – it will be inserted the way it is, and if there is <%=body%>, it will be changed with safe symbols.

Let us show the example of:

Of course, we need to transfer a variable to the template. But how can we do it?  The principle is very simple, just write in app.js:

Check it. Enter

http://localhost:3000/

html

As you can see, the first body has been inserted marked with =, while the second – with -.

We’ve got the simplest Express website, with the output into the variable template. In our next article we will work a bit more with templates, front-end part, and then will move to the data.

The lesson code can be found here.
staytuned2

The materials for this article were 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 the correct name.
Please enter the correct email.
Looks good!

Related articles

15.03.2024

JAMstack Architecture with Next.js

The Jamstack architecture, a term coined by Mathias Biilmann, the co-founder of Netlify, encompasses a set of structural practices that rely on ...

19.01.2024

Training DALL·E on Custom Datasets: A Practical Guide

The adaptability of DALL·E across diverse datasets is it’s key strength and that’s where DALL·E’s neural network design stands out for its ...

12.06.2023

The Ultimate Guide to Pip

Developers may quickly and easily install Python packages from the Python Package Index (PyPI) and other package indexes by using Pip. Pip ...

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