★Overview
I tried to summarize it with reference to the tutorial about MDN Express.
See another article about installing Node.
★First about Node.js
JavaScript server-side tool and application.
JavaScript has an image that runs on the browser, but it also runs on the server side
The API for the browser is removed, and the API for the OS (access to files, etc.) and the API group for HTTP are included.
★Advantages of Node.js as a web server
It has throughput and scalability.
Both the browser and server side can be written in JavaScript.
You can reuse good packages with node package manager (NPM). It can also handle package dependencies. easy.
★Simple web server code example
You can easily start a web server with Node’s HTTP module below.
Can be started with $ node app.js
Accessing localhost 8000 will return the Hello World text.
★About Express
Express is the most popular Node web framework.
It’s also the source of other well-known Node web frameworks.
The following mechanism is provided.
Write a handler with HTTP verbs for the request with a different URL path.
I often use rendering engines called “view”. (With GUI)
Handles template locations and common web application settings such as ports.
In the process of handling the request, an additional request processing called “middleware” can be added. (middleware pattern)
Many libraries have been created as the middleware of Express.
Library for cookies, sessions, user logins, URL parameters, POST data, security headers, etc.
★HelloWorld Express (Easy to use)
◉The app (customary naming) object has methods for manipulating the behavior of the application.
- HTTP routing method
- middleware settings
- view rendering
- Template engine registration
- Change application settings, etc.
◉app.get ()
- Define a callback function (function part) that is called every time the “/” path is accessed.
- The callback function has request and response as arguments, and returns “Hello World!” By calling send () with response.
★About Node.js modules
The module is a JavaScript library that can be imported with the require () function.
Express itself is also a module, and DB and middleware libraries can be used in Express applications.From the viewpoint of maintenance, you will want to make your own module.
It can be used as a module by exporting as follows.
★About asynchronous API
Asynchronous processing API is more often used for JavaScript than synchronous processing API.
Node runs in a single thread.
Single thread means that requests to all servers are handled by one thread.
This has advantages in terms of speed and routing, but it has a significant performance impact over time when executing synchronous functions.
There are several ways to get notification of the completion of the asynchronous processing API.
The most common way is to register a callback function that will be called upon completion.
Sometimes I’m addicted to callback hell, but there are solutions that can reduce it, so check it out. (Promise)
★Route handlers
◉In the Hello World example, the callback is implemented by the routing method (get), but there is a method for response other than send ().
- res.json ()
- res.sendFile ()
◉Of course, in addition to get, there are routing methods (corresponding to HTTP verbs).
- post ()
- put ()
- delete () … etc
◉There is also a special routing method called app.all ()
- Called in all HTTP method responses
Group routing handlers are also useful. (express.Router)
Example: a site with a Wiki might have all wiki-related routes in one file and have them accessed with a route prefix of / wiki /
There are two routers.
- ‘/ wiki /’
- ‘/ wiki / about /’
★Use middleware
Middleware is widely used in Express apps.
Serving static files, error handling, etc.
The route function ends the HTTP request-response cycle by returning something to the HTTP client, but the middleware function usually performs some processing during the request and during the response, and the next function (middleware, handler, etc.) To call.
The order in which middleware is called depends on the developer.
If you do not want to end the cycle, you must call the next () method and pass the control to the next middleware function. Note that the request will hang as it is.
Many apps should use third-party middleware.
I want to keep things as simple as possible, such as cookies, logging, and sessions.
To use the third-party middleware, you must first install it on NPM.
(Note): middleware and routing functions are called in the order they are declared.
Note that some middleware is called in an important order.
session middleware depends on cokie middleware → cookie middleware must be called first
You can create your own middleware.
The difference between middleware function and route handler callback is that middleware has a third argument, next.
You can add middleware in a chain with app.use () or app.add ().
How to add depends on “I want to apply middleware to all responses” or “I want to apply it to the response of a specific HTTP verb (GET, POST etc)”.
In either case, you need to specify routes when calling app.use (). Although route is an option.