nodejs basis -2

Modular commonJS

Introduction

JS is mainly to develop the back end of the performance, commonJS a specification nodejs / webpack is a realization

ECMA is a specification js / as he realized

Other modular specifications: seajs.js / require.js CMD / AMD / UMD es5

effect

Is variable with file scope, do not pollute the global variable

System Module

http fs querystring url

Entry

require('模块名')
require('模块名').xx  按需引用

Do not specify a path: go first system modules -> then find node_modules from the project environment | bower_components (dependent on module) -> not found

Specify the path: to find the specified path -> not found

Support any type

Export

exports.自定义属性 =| any

Batch Export all properties

Can output multiple times

module.exports =| any		

Output only once

note

commonJS is nodejs default management module does not support modular management es6, but supports all es6 + syntax

Examples:
MOD / a.js

let a =10;

//要想在别的文件中使用 a 变量 需要 exports导出
let b = "abc"
let fn =()=>{
    console.log("我是一个函数")
}
exports.aa =a//aa是自己定义的名字
exports.b =b 
exports.fn = fn

mod/b.js

let a =10;

//要想在别的文件中使用 a 变量 需要 exports导出
let b = "abc"
let fn =()=>{
    console.log("我是一个函数")
}

//一个Person 类
class Person{
    constructor(){
        console.log("person")
    }
    show(){
        console.log("show")
    }
}

//exports 导出
// exports.aa =a//aa是自己定义的名字
// exports.b =b 
// exports.fn = fn

// module.exports = a //10

// module.exports = b //abc

module.exports ={
    a:a,
    b:b,
    fn:fn,
    Person:Person
}
//对象中 key = value key可以省略 例如a:a 可以直接写为 a

index.js


//引入 a.js文件
// let a = require("./a")//a.js 的.js可以不写


//路径问题 如果 a.js放在别的路径下 需要修改路径

// let a =require("./mod/a")//现在a.js 在 mod文件夹下



// console.log("a",a)

// a.fn()


//引入 b.js文件
let b =require("./mod/b")

// console.log(b)

let person = new b.Person()
person.show()

NPM

effect

Help you install the module (package), automatically install dependencies, package management (add, delete, update, all package items)

There is also similar: Bower the Yarn

Installed in the global environment

  • Installed into the computer system environment
  • Can be used in any position to use
  • The global installed usually: command-line tools, scaffolding
npm i 包名 -g								安装
npm uninstall 包名 -g	 			卸载

Installation to the project environment

Use only in the current directory, you need to run on behalf of npm

Initialize the project environment

npm init

Npm management initialization file package.json

package-lock.json dependent files used to cure

{
  "name": "npm",	//项目名称
  "version": "0.0.1",	//版本
  "description": "test and play",	//描述
  "main": "index.js", //入口文件
  "dependencies": {  //项目依赖  上线也要用
    "jquery": "^3.2.1"
  },
  "devDependencies": { //开发依赖 上线就不用
    "animate.css": "^3.5.2"
  },
  "scripts": {	//命令行
    "test": "命令行",
  },
  "repository": {	//仓库信息
    "type": "git",
    "url": "git+https://github.com/alexwa9.github.io/2017-8-28.git"
  },
  "keywords": [  //关键词
    "test",'xx','oo'
  ],
  "author": "wan9",
  "license": "ISC",	//认证
  "bugs": {
    "url": "https://github.com/alexwa9.github.io/2017-8-28/issues"//问题提交
  },
  "homepage": "https://github.com/alexwa9.github.io/2017-8-28#readme"//首页
}

Project Dependencies

Only be used in the current project, on the line, this also needs to rely on --save

//安装
npm i 包名 --save
npm install 包名 -S
npm install 包名@x.x.x -S

//卸载
npm uninstall 包名 --save
npm uninstall 包名 -S

Development dependence

Only be used in the current project, on the line, do not need to rely on the --save-dev

npm install 包名 --save-dev
npm install 包名 -D

View package

npm list  列出所有已装包
npm outdated 版本对比(安装过得包)
npm info 包名 查看当前包概要信息 
npm view 包名 versions 查看包历史版本列表

And install any dependencies

npm install 

All the packages installed inside the specified package.json

Version constraint

^x.x.x   约束主版本,后续找最新
~x.x.x   保持前两位不变,后续找最新
*		 匹配任何版本
x.x.x 	 定死了一个版本

Select source

npm install nrm -g     安装选择源的工具包
nrm ls 查看所有源
nrm test 测试所有源
nrm use 切换源名

Release package

  • Official website registration
  • log in
    • npm login log in
    • Enter user / password / email
  • Creating a Package
    • npm init -y
    • Creating entrance index.js
    • Write, output
  • release
    • npm publish
  • Iteration
    • Revision number
    • npm publish
  • delete
    • npm unpublish

Package release, iteration, delete, needs to be in the package directory

Delete packages sometimes need to send a message

YARN

Official website

installation

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, global post-installation package by yarn convenience

use

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, peerDependenciesand optionalDependenciescategories:

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

Official website

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 third-party package management profiles

Project Dependencies

Only be used in the current project, on the line, this also needs to rely on --save

//安装
同npm
bower install 包名#x.x.x -S 指定版本使用#

//卸载
同npm

Development dependence

Only be used in the current project, on the line, do 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'));

example:


//引入 express框架
const express = require("express")


//创建 web服务器
const app = express()

//监听端口

app.listen(3000,"localhost",()=>{
    console.log("监听端口3000")
})

//资源托管
// express.static("")

app.use(express.static("./www"))

//处理接口响应
app.get("/api/goods",(req,res)=>{
    console.log("godds接口")
    res.end()
})
Released nine original articles · won praise 0 · Views 94

Guess you like

Origin blog.csdn.net/weixin_43861707/article/details/104786649
Recommended