Reader small image

You're reading from  Mastering TypeScript - Fourth Edition

Product typeBook
Published inApr 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781800564732
Edition4th Edition
Languages
Right arrow
Author (1)
Nathan Rozentals
Nathan Rozentals
author image
Nathan Rozentals

Nathan Rozentals has been writing commercial software for over 30 years, in C, C++, Java and C#. He picked up TypeScript within a week after its initial release in October 2012 and realized how much TypeScript could help when writing JavaScript. He was one of the first people to start blogging about TypeScript, discussing early frameworks such as Backbone, Marionette, ExtJS and AngularJs. He knew he'd hit the mark when Microsoft staff started to reference his blog posts in their CodePlex discussion forums. Nathan's TypeScript solutions now control User Interfaces in IoT devices, run as stand-alone applications for Point-of-Sale solutions, provide complex application configuration web sites, and are used for mission-critical server APIs.
Read more about Nathan Rozentals

Right arrow

Node and Express

JavaScript has traditionally been used within web browsers in order to enhance or improve the usability or style of web pages. With the growth of the web, each browser competed to provide the best and fastest JavaScript engine they could. One of these JavaScript engines was the V8 engine, which was initially built for Google Chrome, and was released as open source in 2008. Using this engine, Ryan Dahl wrote a JavaScript engine that could be used as a web server, and run on the command line, named Node.js, or simply Node. A year after Node was released, the first versions of the Express framework for Node were released, which provided a set of features to simplify building server-side applications using JavaScript.

Node, using the single-threaded execution of JavaScript code, is able to handle thousands of concurrent web server requests from servers that are relatively small and cheap. Using the callback mechanisms of JavaScript, this also means that programmers...

Express introduction

In this section of the chapter, we will set up a Node and Express development environment, and show how to build the simplest web server with just a few lines of code. We will then discuss routes, and show how to split up our code base into modules for ease of maintenance and readability. Finally, we will discuss how to set configuration parameters for a Node application.

Express setup

In order to build a Node and Express application, we just need to initialize a Node environment, and install a few npm packages, as well as their corresponding declaration files as follows:

mkdir node-express-app
cd node-express-app
npm init
npm install express
npm install @types/express --save-dev

Here, we have created a directory named node-express-app, changed into this directory, and then initialized a Node environment. We then install the express module using npm, and install the TypeScript declaration files for Express, as in @types/express. We will also need...

An Express application

Now that we have an idea about the basics of Express routing and setup, let's build a two-page application that handles a user login form. Along the way, we will learn about how Express renders application HTML pages, how they can be combined with a generic layout page, and how to serve static files such as CSS and icons. We will also discuss how to handle form input, and how to use session data.

Express templating

Our current route handlers are returning simple messages to the browser. In a real-world application, however, we will need to render complete HTML pages, with a standard HTML structure, including links to CSS stylesheets if necessary, and a header section. Express uses a template engine that allows us to specify the HTML we need for each page, and also provides the mechanism of injecting run-time values into these templates, similar to the template mechanisms of Angular, React, or Vue.

Express supports many different templating engines...

Summary

In this chapter, we explored writing a Node application, and particularly a web application, using the Express library. We worked through setting up a very minimal Express application, and then went on to discuss Express routes and configuration. We then explored Express template engines, and installed and configured Handlebars to render two different pages, through our route handlers. We then discussed static files and implemented a login page, showing how to write data to session information. In the next chapter, we will explore the world of REST APIs, and build an API using Amazon Web Services.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering TypeScript - Fourth Edition
Published in: Apr 2021Publisher: PacktISBN-13: 9781800564732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Nathan Rozentals

Nathan Rozentals has been writing commercial software for over 30 years, in C, C++, Java and C#. He picked up TypeScript within a week after its initial release in October 2012 and realized how much TypeScript could help when writing JavaScript. He was one of the first people to start blogging about TypeScript, discussing early frameworks such as Backbone, Marionette, ExtJS and AngularJs. He knew he'd hit the mark when Microsoft staff started to reference his blog posts in their CodePlex discussion forums. Nathan's TypeScript solutions now control User Interfaces in IoT devices, run as stand-alone applications for Point-of-Sale solutions, provide complex application configuration web sites, and are used for mission-critical server APIs.
Read more about Nathan Rozentals