Reptiles, modular front end, three types Node.js

1.http reptiles, http web server to send data to render the foreground, Chinese garbled
http module data request: get request post
case: the back-end reptile
process: the back-end data request - "Data Analysis -" data cleansing - "send data reception

2.http web server sends the rendering data reception
1. introducing http module
2. You can create a web server via http module body createServer this API
3. Create a domain name and server port

Server:
you can run on a server website (site)
Type:

  1. web server (static server) can run in the browser server
  2. api server (backend interface) exposing a rear end of the data interface language, for the front end of a data request (Ajax FETCH)
    Node.js created natively web server
    http module
    the createServer (callback) to create a server
    callback receives three parameters Response Request
    the listen ( port, host, callback) monitor server (server status feedback)
    port port
    host domain name

3. Chinese distortion
Node.js in Chinese distortion
1. Set request header
response.writeHead (200 is, {
'the Type-the Content': 'text / HTML; charset = the UTF8' // lowercase may UTF8
})
2. Send a meta tag
response.write ( '')

3. toString() 在当前应用场景中还不行
二进制有效
将二进制 --- 》 string

Noun: chunk sliced

We have just sent a html to the front, then this form of rendering data we call: 'backend rendering', also known as: 'server-side rendering', English is called: 'ssr'

  1. The modular front end

    Question: Why do you want to use front-end modular?

    Modular: an object (broadly) having a specific function

Defined process module:

1. The definition module (object)

2. Export module

3. reference module

Benefits:

  1. You may store a plurality of individual function blocks

    		2. 复用性高
    
  2. kind

    1. AMD( require.js)
    2. CMD ( sea.js )
    3. Common.js
  3. AMD definition of a module

    define

    // AMD
    /*
    	目录
    		admDir
    			a.js
    			index.js
    */
    // AMD定义  a.js
        define ({
            a: 1,
            b: 2,
            add: function(){}
        })
    // AMD引用 index.js
    	require([./a.js],function( moduleA ){
            //moduleA指的就是定义来的对象
    	})
    
  4. CMD definition module

    define

    //CMD
    /*
    	目录结构
    		b.js
    		index.js
    */
    
    // 模块定义  b.js
    	define(function(require, exports, module) {
    
        	// 模块代码
    
        });
    //模块引用 index.js
    	require('./b.js',function( moduleB ){
            //moduleB就是b模块中导出的内容
    	})
    

    One face questions: CMD AMD gold

  5. Common.js

    Node.js uses Common.js specification (will be the)

    Fallible understanding:

    common.js the node belongs ×

    node belongs common.js ×

    //common.js
    /*
    	目录结构:
    		name.js
    		index.js
    */
    //模块的定义 name.js
    	const nameObj = {
            name: 'Gabriel Yan '
    	}
    //模块的导出  name.js
    	module.exports = nameObj
    //模块的引用
    	const nameObj = require('./name.js')
    

Node.js Common.js specification in use are of three types:

  1. Built-in module (mounted built-in module refers to the global object body Node.js API)

  2. Custom Modules

    1. Module Definition

      //举例
      const student = {
          id: 1,
          name: 'Gabriel Yan'
      }
      const fn = function(){}
      
    2. Export module

      // 第一种导出
      module.exports = student // 安全性不高  默认导出一个
      //第二种导出
      module.exports = { //批量导出,按需引用
          student,fn
      }
      
    3. Reference module

      // 这种引用对应第一种导出
      const student = require('./xxx.js')
      // 这种引用对应第二种导出
      const { student , fn } = require( './xxx.js ' )
      

      note:

      When a custom module reference, require write path must be

  3. Third party modules

    1. 别人已经封装好的模块
    2. 这个模块具备一些特定的功能
    3. 这些模块存放在  www.npmjs.com 这个网站中
    这些模块的文档也记录在内
    
    格式: var/let/const 变量名 = require( 模块名称 )
    
      总结: 
          第一步,使用npm/cnpm 安装  例: npm i request -D
          第二部,在文件中引入
          第三部,在www.npmjs.com这个网站中找到这个模块的文档,根据文档来使用
    

Thoughts: We are not able to package their own custom modules into third-party modules?

analysis:

Third-party modules have the characteristics:

  1. There are certain functions
    2. store in: http://www.npmjs.com in

Solution: Upload custom module ( http://www.npmjs.com )

  1. Create a file package.json

    $ npm init -y
    
    
  2. Register an account on this site http://www.npmjs.com

    Note: the first landing will send a message to your mailbox (this is sent manually), then activate the landing-mail

  3. Check your source computer is not the source npm

    $ nrm ls
    
    

    If so, it will not bother

    If not, then switch to the source npm

  4. Command line login npmjs warehouse

    $ npm adduser
    
    
  5. Create a module and export module

  6. See if your name is already in use package

  7. Release package

    $ npm publish
    
    

Err points:

  1. E-mail is not activated (up)
  2. No handover source npm
  3. Npm entire computer can not be used

If the above method does not solve:

You use someone else's account to try it

-D === --save-dev

//举例

cnpm  i  jquery -D  === cnpm i jquery --save-dev  // 开发环境下使用
cnpm i jquery -S ===  cnpm i jquery --save // 生产环境下使用

Node.js cross-domain

Cross-domain;

The front cross-domain

jsonp

Reverse Proxy (create a virtual back-end servers, back-end server to help us make this request data)

Back-end cross-domain

​ node php java

  1. Setting request header

  2. Using third-party middleware (cors)

    Middleware: is a function of a certain function

Guess you like

Origin blog.csdn.net/weixin_45213847/article/details/93302724