The installation

  1. Install express frame and Express-generator scaffolding
$ npm install express --save -g
$ npm install express-generator --save -g
Copy the code
  1. Once the Express-Generator is installed, you can use it to create an Express project
Express "Your project name"Copy the code

In win10, you can run the PowerShell as an administrator, enter set-executionPolicy remotesunet, and enter Y enter

  1. After installing the dependencies, you can start the project directly
CD "your project" NPM install NPM startCopy the code

Start your browser and type //localhost:3000 to access it

The initial directory structure of the project is not complicated, in fact, if you have used or are familiar with vue-CLI should be able to get started quickly.

/bin/ WWW: HTTP module service configuration related /public: stores public static files such as images, JS, and CSS /routes: Route configuration /views: front-end template file, such as the HTML template filled when the project runs incorrectly /app.js: /package.json: node project file. This file usually contains project dependencies, name, version, description, command line abbreviations for executing scripts, etc

So how does the project work when we type NPM start? NPM start is a default startup command in a common project, which is configured in package.json. You can see that the value of start is node. Node./bin/ WWW will be executed when we type NPM start

NPM start = NPM run start = NPM stop = NPM test

/bin/ WWW will start an HTTP service that listens on port 3000 by default, which is why we can access it via localhost:3000. There is a sentence in the file http.createserver (app) that uses the app.js export module as a listener for the HTTP service request event.

App.js, on the other hand, creates and exports an instance of Express, the listener function just mentioned. It includes route monitoring and distribution, error capture, cookies, and so on.

What about the routing monitoring and distribution process? Suppose we type //localhost:3000/users in the browser address bar

The browser address bar sends a GET request, so follow the instructions in app.js

var usersRouter = require('./routes/users');
app.use('/users', usersRouter);
Copy the code

The request will be sent to the./routes/users routing file

router.get('/'.function(req, res, next) {
  res.send('respond with a resource');
});
Copy the code

This route receives a GET request and eventually returns the text ‘respond with a resource’.

The default receiving function contains three parameters, req: request, which contains the parameters and headers of the request. Res: Response, which contains the information and method of the response. Next: If next() is called, the request is forwarded to the next matching routing control as described in the official documentation

Node.js is based on Google’s V8 engine and is great for front-end developers who are used to writing JavaScript for personal websites or small projects, along with Express.