YARN
-
Quguan network installation
-
Note: To save time, do not use npm i yarn -g, to install the yarn, but to download the archive to ensure hard write to the registry and environment variables, convenient post-installation package by the global yarn
using
Initialize a new project
yarn init
- Add dependencies
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
-
The dependency to different dependencies category
-
They were added to the dependencies, devDependencies, peerDependencies and optionalDependencies categories:
yarn add [package] --save | -S
yarn add [package] --dev | -D
yarn add [package] --peer
yarn add [package] --optional
- Upgrade dependencies
yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]
- Removing dependencies
yarn remove [package]
- All rely installation project
yarn
- or
yarn install
Installed in the global
yarn global add [package] //global的位置测试不能变
yarn global remove [package]
BOWER
- Quguan network installation bower
npm install -g bower
Installation package to the global environment
bower i 包名 -g 安装
bower uninstall 包名 -g 卸载
Installation package to the project environment
- Initialize the project environment
bower init
bower.json 第三方包管理配置文件
- Project Dependencies
Only be used in the current project, on the line, this also needs to rely --save
//安装
同npm
bower install 包名#x.x.x -S 指定版本使用#
//卸载
同npm
开发依赖
Only be used in the current project, on the line, does not need to rely on the --save-dev
同npm
EXPRESS
nodejs library, do the basics, work simplification, click to enter the official website, there are similar koa
Feature
Second package, non-invasive, enhance shape
Build a web service
let express=require('express')
let server=express()
let server.listen(端口,地址,回调)
Static Resources Managed
server.use(express.static('./www'));
The interface response
Supports a variety of postures request: get, post, put, delete ...
server.请求姿势API(接口名称,处理函数)
server.get(url,(req,res,next)=>{})
server.post(url,(req,res,next)=>{})
req request body
The HTTP request object request, the request comprising a query string parameter, contents, HTTP header attributes
req.query //获取地址栏的数据
req.body //获取非地址栏的数据 依赖中间件
req.params //获取动态接口名
req.method //获取前端提交方式
req.body rely on middleware
Middleware Use: body-parser
npm install body-parser
let bodyParser = require(‘body-parser’)
app.use(bodyParser ())
res response body
response HTTP response data object represents an HTTP response, i.e., transmitted to the client upon receiving the request
res.send(any) //对等 res.write + end
res.end(string|buffer)
res.json(json) //返回json
res.status(404).send({error:1,msg:"Sorry can't find that!"}) //返回一个404
res.jsonp(响应数据) //调用请求时的回调函数并传递响应数据
res.sendFile(path.resolve('public/error.html'))//渲染纯 HTML 文件
jsonp response
server.set('jsonp callback name','cb')//默认callback
server.get('/jsonp接口',(req,res,next)=>res.jsonp(数据))
Processing part of the interface
Total business logic together to deal with the
server.all('/admin/*',(req,res,next)=>{}))
all matching full path for all HTTP
Need to continue follow-up next
use
Installation middleware, routing, takes a function,
server.use([地址],中间件|路由|函数体)
Middleware
Middleware, custom business processing, only the processing request to the intermediate portion of the end of the response
For example
npm i body-parser -S //安装包
let bodyParser=require('body-parser')//引入中间件
server.use(bodyParser())//安装中间件
body-parser 使用方式,实时查询 npm,可获得最新
Jump back end
res.redirect(url) 指向一个接口
Spread
req
- req.app: callback when an external file, accessible by express instance req.app
- req.baseUrl: get URL routing currently installed
- req.cookies:Cookies
- req.fresh / req.stale: to determine whether the request is still "fresh"
- req.hostname / req.ip: Get names and IP addresses
- req.originalUrl: get the original request URL
- req.path: Get request path
- req.protocol: Gets the protocol type
- req.route: Get the current matching route
- req.subdomains: get subdomain
- req.accepts (): checking the type of document requests acceptable
- req.acceptsCharsets / req.acceptsEncodings / req.acceptsLanguages: Returns the specified character set of a character encoding acceptable
- req.get (): Get the specified HTTP request header
- req.is (): if the request Content-Type header MIME type
res
- res.app: Like req.app
- res.append (): Specifies additional HTTP header
- res.set () before resetting the head after res.append () provided
- res.cookie(name,value [,option]):设置Cookie
- opition: domain / expires / httpOnly / maxAge / path / secure / signed
- res.clearCookie():清除Cookie
- res.download (): file transfer path specified
- res.get (): returns the specified HTTP header
- res.location (): Set Location HTTP response header only, is not provided or close response status code
- res.render (view, [locals], callback): rendering a view, while the rendered string is transmitted to the callback, if an error occurs next (err) during the rendering process will be automatically invoked. callback will be passed in a possible error occurred and after rendering the page, so it will not automatically output.
- res.sendFile (path [, options] [, fn]): Specifies the transmission path file - are automatically set based on the file extension Content-Type
- res.set (): Set HTTP header, passing object may be provided a plurality of heads
- res.status (): Set HTTP status code
- res.type (): Set Content-Type of the MIME type