Preface
After the previous article "A Brief Discussion on the Components of Index.js, the main file of Nodejs application", I finally got to know the face of the Node girl, and then it seemed to be on <( ̄ ̄)>. Yoxi~ This time I let Ben wander around her body, it's good to think about it. Well, YY ends here.
text
This article mainly uses components to learn about the main file index.js...how smooth the skin is ( ̄ ̄)~
Since I have mentioned the components of index.js before, then this time I will talk about it one by one. That's right! How can my handsome hands let go of every inch of your skin?
1. Introduce dependency modules
Well...it's like this:
var express = require('express');var path = require('path');var favicon = require('serve-favicon');var logger = require('morgan');var cookieParser = require('cookie-parser');var bodyParser = require('body-parser');var bodyParser = require('body-parser');Actually, relying on modules is literal.
In the words of front-end engineers, the code $("#id").attr("class","active") cannot be turned without Jquery.
In the words of animals, you can't survive without food.
In the words of this article, you can't survive without a girl.
Well~ To sum up, dependence is to prepare for what you need to do next. So the "pinyin" above plays this role...
2. Set up related configurations
Deliver the relevant codes of this group:
// View engine settings app.set('views', path.join(__dirname, 'views'));app.set('view engine', 'jade');Well...just two lines of code, my breasts are relatively small ( ̄ ̄), after all, loli, hahahaha.
The above two lines of code are to set the attempt directory and set the jade as the attempt engine. The amount of settings depends on which modules you want to use. Some modules also have default settings, but for the convenience of writing by yourself, they will definitely set them to the way you like.
3. Middleware
// Middleware app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));app.use(logger('dev'));app.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: false }));app.use(cookieParser());app.use(express.static(path.join(__dirname, 'public')));Middleware, Strictly speaking, every line in index.js is middleware. I guess many brothers know the word middleware, but they still don’t understand what it means. What it mainly does.
So, I personally began to draw the only picture in the universe:
I believe that smart people can understand it at once. After all, it can be worth 50 cents when you sell it.
Seriously speaking: The big arrow on the left refers to the code execution order. Everyone knows that Js is executed from top to bottom. For the sake of explaining the middleware, the big arrow is regarded as a flow of water.
While the water flowed downward, something similar to the filter stuck in the middle. Ben suddenly thought about whether the hymen was the same (ah, my mind was not right again).
The black filter is the code above. The function of the middleware is the police, doorman or guide before, during and after the main code is executed. For example, for example, using the bodyParser middleware, its function is to parse the body data analysis of HTTP requests, which is convenient for use in routing. The same is true for cookie-parser.
In the middle part of the two filters in the figure, it is generally a route, and it is responsible for returning the page that the client wants.
At this time, he asked with a sharp eye, why is the middleware below red? And why do I put it behind the route to execute?
Good question! I'm about to say it. Friends who are familiar with Node development know that most middleware is generally written before routing, but sometimes some uncertain errors cannot be avoided, such as what should I do if the page the user visits does not exist? What should I do if the development version is wrong or the official version is wrong? At this time, it is the "red" middleware turn to appear. Yes, it is mainly responsible for the aftermath and is generally used to deal with possible errors.
4. Routing
//Route app.use('/', routes);app.use('/users', users);Router is a good person. The client only needs to send a request, and the router will try its best to help you solve the request based on the request url. It can process data in different ways (get, post, put, del...) or return to the page you want. Anyway, it is a good silver that can be tried to get back to the best of your questions~ In fact, routing is also a middleware, but it should be better as an independent component.
5. Turn on the service
//Start the service http.createServer(app).listen(app.get('port'), function () { console.log('The service started successfully, the port is: '+app.get('port'));});Ah hahahaha, I finally reached orgasm, Node girl is almost out of reach <( ̄ ̄)>
Forehead. . . There seems to be nothing to say, just to enable the service. . . . . . Well, before starting the service, you need to create a service through http.createServer(), and then call the listen() method to enable it. In this way, the entire application will be opened~~~ The big arrow in this magical picture can also be penetrated smoothly. . Go in. . Go. . Now.