Talking about Nodejs application main file Index.js_node.js

Source: Internet
Author: User

Objective

After the previous article "on the Nodejs application of the main document Index.js part of the", finally met the girl's face, and then as if on the "( ̄︶ ̄)." Yo-XI ~ this time let the cock in her carcass upstream walk some, think is also good drop. Well, yy ends here.

Body

This article mainly takes the component as the unit to understand index.js this main file ... How slippery the skin is ( ̄▽ ̄) ~

Now that I have said the components of index.js, then this is a group of groups. That's right! This handsome hand, how can let your every inch of skin!

First, the introduction of dependency modules

Well.. Just 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 ');

In fact, the dependency module is literally meant to be.

In the case of a front-end engineer, it is $ ("#id"). attr ("Class", "active") the code does not turn on without jquery.

In the words of animals, there can be no living without food.

In the words of this cock, no sister can live.

Well, to sum up, Reliance is to prepare for the next thing to be done. So the above "pinyin" is to play this role.

Second, set up related configuration

Present this group of related code:

The view engine settings
app.set (' views ', Path.join (__dirname, ' "));
App.set (' View engine ', ' Jade ');

Amount of ... Two lines of code, the chest is relatively small ( ̄﹏ ̄), after all, Lori, aha.

The above two lines of code are set to attempt the directory and set Jade as an attempt engine. The number of settings depends on which modules you want to use, and some of the modules have the default settings, but for their own convenience, it will be set to their liking.

Third, 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, each line of index.js is middleware, I guess a lot of brothers know the word middleware, but still do not understand what it means, what it is mainly doing.

So, the cock himself began drawing a picture of the only one in the universe:

I believe that smart people can understand, after all, this cock painting to sell can also be worth a 5 cents.

Serious words: The left Big Arrow refers to the code execution order, everyone knows that Js is executed from the top down, here in order to explain the middleware, the big arrows as water.

And in the process of flow to the dirty, there is a similar screen in the middle of a layer of cards, this cock suddenly thought of the hymen is not also so (Ah Pooh, the idea is not  ̄▽ ̄).

Black screen, is the above code, the role of middleware in the implementation of the main line of code before the process, process, the process of the police, doorman or guide. To give a chestnut, for example, using Bodyparser middleware, its role is to parse the HTTP request of the body data parsing, convenient in the use of the route, Cookie-parser is the same truth.

In the diagram, the middle part of the two filters is generally routed, which is responsible for returning the page the client wants.

At this time, the eagle-eyed asked, the following middleware why is red? And why do you want to put it behind the route?

That's a good question! I was going to say it. Familiar with node development partners know that most of the middleware is generally written in the route before, but sometimes not to avoid some uncertain errors, such as user access to the page does not exist? What if the development version is wrong or the official version is wrong? This is the turn of the "red" middleware to appear, yes, it is mainly responsible for the aftermath, generally used to deal with possible errors.

Four, routing

Routing
app.use ('/', routes);
App.use ('/users ', users);

The route is a good guy, the client only sends the request, the route will try to help you to solve the request according to the request URL, it can use different way (get, post, put, Del ...). To process the data or return to the page you want. In short, it is a good silver to ask the best to return the route is also middleware, but it should be as an independent component better.

V. Opening of services

Start the service
Http.createserver (APP). Listen (App.get (' Port '), function () {
  console.log (' service started successfully, port is: ' +app.get (') Port ');
});

Ah ha ha, finally to climax, node sister fast < ( ̄︶ ̄) >

Amount of ... Seems to have nothing to say, is to open the service ... OK, before you turn on the service, create a service by Http.createserver () and call the Listen () method to open it. So the entire application on the Open ~ ~ ~ this cock that picture of the big arrow can also be smooth poke in the. Go in now. Went to the. Out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.