Express Js Introduction

Express, a framework that sits on top of Node.js’s web server and makes it easier to use Middleware and routing, two features of Express.

JavaScript might not be the perfect language for all, but Node.js has real benefits. For one, the V8 JavaScript engine is fast, and Node.js encourages an asynchronous coding style, making for faster code while avoiding multithreaded nightmares. The biggest benefit of Node.js is the ability to share code between browser and server. Developers don’t have to do any kind of context switch when going from client and server. Now they can use the same code and the same coding paradigms between two JavaScript runtimes: the browser and the server.

Comparing asynchronous code (like Node.js) to synchronous code. Note that asynchronous code can complete much faster, even though we’re never executing your code in parallel. It doesn’t mean to tell that Node.js is the fastest in the world because of its asynchronous capabilities. There is a lot of other programming languages that allows only synchronous. Node.js can squeeze a lot of performance out of one CPU core, but it doesn’t excel with multiple cores because of the node work as single thread programming.

Note: The JavaScript function that processes browser requests in your application is called a request handler. Request handler functions take two arguments: the request and the response.

Express’s minimal philosophy

Express is a framework, the developer doesn’t have to follow some rigid structure or standard. Express is flexible, allow developers, the large numbers of other libraries that we integrate into our Express applications.
But this minimalism is a double-edged sword. It’s flexible and your apps are free of unused cruft, but it does very little for you in comparison to other frameworks. This means that you make mistakes, you have to make far more decisions about your application’s architecture, and you have to spend more time hunting for the right third party
modules.

The core parts of Express

The Express has just four major features: middleware, routing, sub-applications, and conveniences.

Middleware

Node.js gives you one request handler function to work with. The request comes into your function and the response goes out of your function. In express rather than one monolithic request handler function, you call several request handler functions that each deal with a small chunk of the work. These smaller request handler functions are called middleware functions, or middleware.
Example Middleware can handle tasks from logging requests to sending static files to setting HTTP headers. The first middleware function you might use in an application is a logger— it logs every request that comes into your server. When the logger has finished logging, it will continue on to the next middleware in the chain. This next middleware function might authenticate users. If they’re visiting a forbidden URL, it will respond with a “not authorized” page. If they are allowed to visit it, they can continue to the next function in the chain.

Routing

Like middleware, it breaks the one monolithic request handler function into smaller pieces. Unlike middleware, these request handlers are executed conditionally, depending on what URL and HTTP method a client sends.

Subapplications

Express applications can often be pretty small, even fitting in just one file. As the applications get larger, we can break them up into multiple folders and
files. Express is unopinionated about how you scale your app, but it provides one important feature that’s super helpful: sub applications. In Express lingo, these miniapplications
are called routers.

Express is often used to power single-page applications (SPAs). SPAs are very
JavaScript-heavy on the front end and they usually require a server component. The server is usually required to simply serve the HTML, CSS, and JavaScript, but there’s often a REST API, too. Express can do both of these things quite well; it’s great at serving HTML and other files, and it’s great for building APIs.

Note: An important thing to note: there’s no such thing as an Express module—only a Node.js module. A Node.js module can be compatible with Express and work well with its API, but they’re all just JavaScript served from the npm registry, and you install them the same way. Just like in other environments, some modules integrate with other modules, where others can sit alongside. At the end of the day, Express is just a Node.js module like any other.

Resources

Express example – we can also find example applications all throughout the Express repository, at https://github.com/strongloop/express/tree/master/examples.

Express hello world example code.

var express = require("express");
var app = express();
app.get("/", function(request, response) {
response.send("Hello, world!");
});
app.listen(3000, function() {
console.log("Express app started on port 3000.");
});