Express: Node.js-based back-end framework. introduce

Implement a HELLO WORLD

NPM init 3. NPM I Express-s 4. Hello world

var express = require('express');/ / introduce exrpess
var app = express();// Create an instance

/ / routing. When the URL matches the route here, it's handled by the following function
app.get('/'.function (req, res) {
  res.send('Hello World! ');
});

/ / create a server
var server = app.listen(3000.function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});
Copy the code

How does this look like creating a static server using Node.js? Emm because Express is based on the Node.js platform. This essentially creates a local server that listens on port 3000. The server obtains the suffix of the URL, matches the route, and then hands it to the corresponding function for processing.

Express Application Generator?

The server above looks very thin ah, if I want to achieve login registration and so on do not have to write a lot of? Express Application Generator (Express-Generator

  1. npm i express-generator -s introduce
  2. Example commands:Usage: express [options] [dir]
  3. express --ejs .(In the current directory, create scaffolding and select template.ejs.)

[image-568d2C-1525946563984] At this time we have a lot of new files in the directory, check the package.json, we found that we have a lot of new dependencies, first install various dependencies. npm i

  1. Run the following command to start the servernode bin/wwwThe default port is 3000. If you want to change the port, you can specify the port before the commandPORT=XXXX

What about app.js?

  • Bin/WWW is actually used to build the server and listen for ports. When a request is received, it is handed to app.js for processing.

  • App.js is actually the body of the site, which contains the content that the site depends on, the logic for handling requests, and the execution process. The app in app.js is actually an instance of Express.

  1. Set the view engine to EJS
  2. Set up the flow of middleware processing requests based on the business logic.
  3. Set the static resource directory to return static resource content when a request matches a path in the static resource directory. (E.g., when requestingxxx.com/css/style.cssWe happen to have a CSS folder in our public directory, where style.css is stored. The server will return this file.
  4. Different routes are matched according to the request suffix, and the processing of routes requires us to require in app.js in advance.
  5. If none of the above routes match, we return 404. The requested item does not exist.

What is middleware?

Here we refer to the Chinese document

Middleware functions can access request objects (REQ), response objects (RES), and the next middleware function in the application’s request/response loop. The next middleware function is usually represented by a variable named next.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and response objects.
  • End the request/response loop.
  • Call the next middleware function in the stack.

If the current middleware function does not end the request/response loop, it must call next() to pass control to the next middleware function. Otherwise, the request remains pending.

What about the Router method?

next

function Middleware(request, response, next) { 
   next('Wrong! ');
}
Copy the code

After an error is thrown, subsequent middleware will not execute until an error handler is found. If the next method is not called, subsequent registered functions will not be executed.

Using a template engine

When we first generated the framework with Express-Generator, we entered EJS, which is the template engine I specified. Through the template engine, we no longer write HTML content inside to die, but through the router dynamically pass data to the template engine, make it according to our incoming data dynamically rendering the page, we can even pass judgment statements to different login state user rendered page, through the loop statement to the page to add content, etc., In the router image above

res.render('index', {title: 'Express'})
Copy the code

So we’re going to pass a parameter to the template engine, select the index template, and pass in an object with key as title and value as a string. Then we just need to use the corresponding syntax in the views index.ejs to dynamically render.