Xpress front-end record --------- nodejs based learning framework -Express

Express framework Introduction

  1. What express framework?
  2. Why use a framework express it?
  3. How to use express frame it?

What express framework?

  Express on a frame nodejs developed (http based module package more powerful).
  Express is a flexible and keep the minimum size of Node.js Web application development framework that provides a powerful set of features for Web and mobile applications.

Why use a framework express it?

          Accelerate development programs to facilitate teamwork.
         Express provides a streamlined basic Web application functionality, but does not hide your understanding and favor of Node.js function.

How to use express frame it?

 

 

 Express Getting Started

  1. express Getting Started
  2. express route
  3. What is routing?
  4. Why use routing?
  5. How to use routing?
  6. What is the routing parameters?

The Express Route 

  When the route is a browser enter the URL address, the application requests to which background processing is called routing. And this operation has to be defined in the background rules, rule out the definition of what we call routing rules.

 

How to use routing? 

Route basic grammar

1. The general syntax

app.HTTP request type (request path, a callback function)
 
Sending a GET request: app.get (request path, a callback function)
 
Send a POST request: app.post (request path, a callback function)
 
Send any request: app.all (request path, a callback function)
 

2. special syntax

app.use (request path, a callback function)

+ Difference 1: use match any type of request
+ Difference 2: use a non-exact match (ps url only path to match the front matched requests.)
 
 

What is the routing parameters?

 Routing parameters

  When arguments are passed get name = andy & age 12 behind the url = manner, if the server needs to be acquired, may be obtained by routing parameters.


The basic syntax:
 
app.HTTP request type (request path /: 1 /.../ parameters: the parameter n, the callback function)
 
 

Hosting static files using the Express

 
 
 
 
 

 Project deployment

Simple deployment project

1. The project files stored centrally under the same catalog templates and index.js

2.templates store the same directory (.css) (. Js) (.imgs) (.json) and html

3.npm download dependencies

var http = the require ( 'http'); // introduced http module 

var fs = the require ( 'fs'); // introduced fs module 

// create a service 
var Server http.createServer = ( function (REQ, RES) { 
    
    var = requestUrl req.url;
         // indexOf requestUrl determining whether it contains ".html", if not, -1 is returned 
    IF (requestUrl.indexOf ( 'HTML.')> -1 ) {
         // ./templates/index.html 
        fs.readFile ( './ Templates' + requestUrl, 'UTF8', function (error, dataString) {
             // method fs module reads the file readFile 
            res.end (dataString);
             //响应结束返回内容
        });

    }else if( requestUrl.indexOf('css/') > -1 ){
        fs.readFile('./templates' + requestUrl, 'utf8', function (error, dataString) {

            res.end( dataString );

        });
    }else if( requestUrl.indexOf('js/') > -1 ){
        fs.readFile('./templates' + requestUrl, 'utf8', function (error, dataString) {

            res.end( dataString );

        });
    }else if( requestUrl.indexOf('images/') > -1 ){
        fs.readFile('./templates' + requestUrl, function (error, dataString) { 

            res.end (dataString); 

        }); 
    } the else { 
        res.end ( '404' ); 
    } 

});     
// Set the port as 8080, or other value 
var port = 8080 ;
 // listening port port, 
server.listen (port, function () { 
    the console.log ( `Server running AT $ {port} ...`); 
})

 

 

 
 
 
 
 

Guess you like

Origin www.cnblogs.com/hudunyu/p/11391448.html