[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 }; ```