[TOC]
# :-: 藍奏云解析
:-: module - reqP.js
```
const request = require("request");
module.exports = function reqP({ url = '', type = 'GET', data = '', headers = {} }) {
const options = {
url,
method: type,
charset: "utf-8",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36",
...headers
},
body: data
}
return new Promise((resolve, reject) => {
request(options, function(error, response, conent) {
error ? reject(error) : resolve(conent, response);
});
});
}
```
:-: primary - index.js
```
const cheerio = require("cheerio");
const reqP = require('./reqP.js');
const lanzous = {
parse: function (url, func) {
return reqP({ url }).then(data => {
let $ = cheerio.load(data);
let url = $('body iframe')[0].attribs.src;
return reqP({ url: 'https://www.lanzous.com' + url });
}).then(data => {
let $ = cheerio.load(data);
let sign = $('script').text();
sign = sign.match(/var sg = '(.*?)';/)[1];
return reqP({
type: 'POST',
url: 'https://www.lanzous.com/ajaxm.php',
data: `action=downprocess&sign=${sign}&ves=1`,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'referer': 'https://www.lanzous.com/fn'
}
});
}).then(data => {
data = JSON.parse(data);
return { url, urlParse: data.dom + '/file' + data.url }
});
}
}
lanzous.parse('https://www.lanzous.com/i9nduvg').then(data => {
console.log(data);
});
```
# :-: 連接mysql數據庫
```
const mysql = require("mysql");
module.exports = {
exec: function(...arg) {
const connection = mysql.createConnection({
host: "127.0.0.1", // IP
port: "3306", // 端口
user: "root", // 賬號
password: "mm123456", // 密碼
database: "__schemas" // 數據庫
});
connection.connect(); // 連接數據庫
connection.query(...arg);
connection.end(); // 關閉數據庫
},
getAll: function(func) {
// 查詢表(student)
this.exec("select * from student;", (error, result) => func(result));
},
set: function(arr, func) {
this.exec(
"insert into student (`stu_num`,`name`,`age`,`class`) value (?,?,?,?);",
arr,
(error, result) => {
func({ state: typeof result == "object", arr });
}
);
}
};
```
# :-: 服務端·接收上傳的文件
```
const multer = require("multer"), // 接收上傳的文件
fs = require("fs"),
url = require("url");
const uploadSingle = multer({ dest: "./write/file/" }); // 配置
module.exports = function(app) {
app.post("/api/upload", uploadSingle.single("myFile"), (req, res) => {
// 接收文件 multer
// console.log(req.body);
// console.log(req.file);
const fileObj = req.file;
const fileName = `${(+new Date()).toString(24)}_${fileObj.originalname}`;
fs.rename(fileObj.path, "write\\file\\" + fileName, err => {
if (err) throw err;
fileObj.path = "/api/file/" + fileName;
res.send(fileObj);
});
});
app.get("/api/file/*", (req, res) => {
// 下載文件
const fileName = req.url.split("/api/file/")[1];
// if (fileName) res.send(fs.readFileSync('./write/file/e90e3i9fd_圖片1.png'));
if (fileName) res.send(fs.readFileSync("./write/file/" + fileName));
});
};
```
# :-: 數據庫連接案例
:-: ../entities/Movie.ts
```ts
import "reflect-metadata";
import {
IsNotEmpty, ArrayMinSize,
IsInt, Min, Max, IsArray
} from "class-validator";
import { Type, plainToClass } from "class-transformer";
import { BaseEntity } from "./BaseEntity";
export class Movie extends BaseEntity {
@IsNotEmpty({ message: "電影名稱不能為空" })
@Type(() => String)
public name: string;
@IsNotEmpty({ message: "電影類型不能為空" })
@ArrayMinSize(1, { message: "電影類型成員至少有一個" })
@IsArray({ message: "電影類型必須為數組" })
@Type(() => String)
public types: string[];
@IsNotEmpty({ message: "上映地區不能為空" })
@ArrayMinSize(1, { message: "上映地區成員至少有一個" })
@IsArray({ message: "上映地區必須為數組" })
@Type(() => String)
public areas: string[];
@IsNotEmpty({ message: "時長不能為空" })
@IsInt({ message: "時長必須為整數" })
@Min(1, { message: "時長至少一分鐘" })
@Max(999999, { message: "時長已達到上限" })
@Type(() => Number)
public timeLong: number;
@IsNotEmpty({ message: "是否熱映不能為空" })
@Type(() => Boolean)
public isHot: boolean;
@IsNotEmpty({ message: "是否即將上映不能為空" })
@Type(() => Boolean)
public isClassic: boolean;
@Type(() => String)
public description?: string;
@Type(() => String)
public poster?: string;
}
```
:-: ./MovieSchema.ts
```ts
import Mongoose from "mongoose";
import { Movie } from "../entities/Movie";
export interface IMovie extends Movie, Mongoose.Document {}
const movieSchema = new Mongoose.Schema<IMovie>(
{
name: String, areas: [String],
tyoes: [String], timeLong: Number,
isHot: Boolean, isComing: Boolean,
isClassic: Boolean, description: String,
poster: String
},
{ versionKey: false }
);
export default Mongoose.model<IMovie>("Movie", movieSchema);
```
:-: ./index.ts
```ts
import Mongoose from "mongoose";
import MovieModel from "./MovieSchema";
Mongoose.connect("mongodb://localhost:27017/moviedb", {
useNewUrlParser: true
}).then(() => console.log("mongodb -- 連接成功"));
export { MovieModel };
```
- 前端工具庫
- HTML
- CSS
- 實用樣式
- JavaScript
- 模擬運動
- 深入數組擴展
- JavaScript_補充
- jQuery
- 自定義插件
- 網絡 · 后端請求
- css3.0 - 2019-2-28
- 選擇器
- 邊界樣式
- text 字體系列
- 盒子模型
- 動圖效果
- 其他
- less - 用法
- scss - 用法 2019-9-26
- HTML5 - 2019-3-21
- canvas - 畫布
- SVG - 矢量圖
- 多媒體類
- H5 - 其他
- webpack - 自動化構建
- webpack - 起步
- webpack -- 環境配置
- gulp
- ES6 - 2019-4-21
- HTML5補充 - 2019-6-30
- 微信小程序 2019-7-8
- 全局配置
- 頁面配置
- 組件生命周期
- 自定義組件 - 2019-7-14
- Git 基本操作 - 2019-7-16
- vue框架 - 2019-7-17
- 基本使用 - 2019-7-18
- 自定義功能 - 2019-7-20
- 自定義組件 - 2019-7-22
- 腳手架的使用 - 2019-7-25
- vue - 終端常用命令
- Vue Router - 路由 (基礎)
- Vue Router - 路由 (高級)
- 路由插件配置 - 2019-7-29
- 路由 - 一個實例
- VUEX_數據倉庫 - 2019-8-2
- Vue CLI 項目配置 - 2019-8-5
- 單元測試 - 2019-8-6
- 掛載全局組件 - 2019-11-14
- React框架
- React基本使用
- React - 組件化 2019-8-25
- React - 組件間交互 2019-8-26
- React - setState 2019-11-19
- React - slot 2019-11-19
- React - 生命周期 2019-8-26
- props屬性校驗 2019-11-26
- React - 路由 2019-8-28
- React - ref 2019-11-26
- React - Context 2019-11-27
- PureComponent - 性能優化 2019-11-27
- Render Props VS HOC 2019-11-27
- Portals - 插槽 2019-11-28
- React - Event 2019-11-29
- React - 渲染原理 2019-11-29
- Node.js
- 模塊收納
- dome
- nodejs - tsconfig.json
- TypeScript - 2020-3-5
- TypeScript - 基礎 2020-3-6
- TypeScript - 進階 2020-3-9
- Ordinary小助手
- uni-app
- 高德地圖api
- mysql
- EVENTS
- 筆記
- 關于小程序工具方法封裝
- Tool/basics
- Tool/web
- parsedUrl
- request