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:
- web server (static server) can run in the browser server
- 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'
-
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:
-
You may store a plurality of individual function blocks
2. 复用性高
-
kind
- AMD( require.js)
- CMD ( sea.js )
- Common.js
-
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指的就是定义来的对象 })
-
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
-
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:
-
Built-in module (mounted built-in module refers to the global object body Node.js API)
-
Custom Modules
-
Module Definition
//举例 const student = { id: 1, name: 'Gabriel Yan' } const fn = function(){}
-
Export module
// 第一种导出 module.exports = student // 安全性不高 默认导出一个 //第二种导出 module.exports = { //批量导出,按需引用 student,fn }
-
Reference module
// 这种引用对应第一种导出 const student = require('./xxx.js') // 这种引用对应第二种导出 const { student , fn } = require( './xxx.js ' )
note:
When a custom module reference, require write path must be
-
-
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:
- There are certain functions
2. store in: http://www.npmjs.com in
Solution: Upload custom module ( http://www.npmjs.com )
-
Create a file package.json
$ npm init -y
-
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
-
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
-
Command line login npmjs warehouse
$ npm adduser
-
Create a module and export module
-
See if your name is already in use package
-
Release package
$ npm publish
Err points:
- E-mail is not activated (up)
- No handover source npm
- 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
-
Setting request header
-
Using third-party middleware (cors)
Middleware: is a function of a certain function