nodejs跟vue的關系,006 - node.js

 2023-10-15 阅读 32 评论 0

摘要:006 - node.js 加解密算法 const crypto = require('crypto')let Encrypt = (data, key) => {const cipher = crypto.createCipher('aes192', key);var crypted = cipher.update(data, 'utf8', 'hex');crypted +&

006 - node.js

加解密算法

const crypto = require('crypto')let Encrypt = (data, key) => {const cipher = crypto.createCipher('aes192', key);var crypted = cipher.update(data, 'utf8', 'hex');crypted += cipher.final('hex');return crypted;
}let Decrypt = (encrypted, key) => {const decipher = crypto.createDecipher('aes192', key);var decrypted = decipher.update(encrypted, 'hex', 'utf8');decrypted += decipher.final('utf8');return decrypted;
}let Random = (Min, Max) => {var Range = Max - Min;var Rand = Math.random();if(Math.round(Rand * Range)==0){return Min + 1;}var num = Min + Math.round(Rand * Range);return num;
}let RandomNum = Random(1000,9999) 
let data = 'xtang'  
let key = RandomNum.toString()
let encrypted = Encrypt(data, key)
let decrypted = Decrypt(encrypted, key)console.log('明文' + data)  //明文xtang
console.log('加密后' + encrypted) //加密后6903756dc076823f5222227fb824180d
console.log('解密后' + decrypted)  //解密后xtang

REST 表述性狀態轉移(REpresentational State Transfer)的簡稱。

https://wenku.baidu.com/view/d14e0bde43323968001c92ad.html
【深入淺出REST】   http://kb.cnblogs.com/page/132129/

REST指一組架構約束條件和原則,滿足約束條件和原則的應用程序設計。

REST是設計風格而不是標準。REST通常基于使用HTTP,URI,和XML以及HTML這些現有的廣泛流行的協議和標準。
資源是由URI來指定。
對資源的操作包括獲取、創建、修改和刪除資源,這些操作正好對應HTTP協議提供的GET、POST、PUT和DELETE方法。
通過操作資源的表現形式來操作資源。

shell

SHELL是由PHP技術作為核心邏輯控制器,采用Think PHP的mvc架構進行構建。

Buffer 是ES6新引入的數據類型

// Buffer 需要 先轉成string
Buffer通過toString()轉成字符串

Node.js

nodejs跟vue的關系、是一個基于 Chrome V8 引擎的 JavaScript 運行環境。 Node.js 的包管理器 npm,是全球最大的開源庫生態系統。

文件操作

006中 move 文件
007 抓取music歌詞

server.get(’/’, (req, res) => { // req 就是php中的 $_REQUEST[‘asddsa’]
res.sendfile(path.resolve(’./views/index.html’));
});

創建服務器

  • express模塊
    • 安裝express項目模板node express
    • npm install

var express = require(‘express’);
var app = express();
var http = require(‘http’);

var server = http.createServer(app);

server.listen(8061,function () {
console.log(‘listen to 8060’);
console.log(‘Listening on %d’, server.address().port);
});

參考

node是啥,【nodejs個人筆記】
http://blog.csdn.net/kongjunchao159/article/category/5760829
【express】
https://expressjs.com/zh-cn/guide/writing-middleware.html
【cookie-parser】
http://blog.csdn.net/sooooooooad/article/details/52154228

【Express】
Express 是一個基于 Node.js 平臺的極簡、靈活的 web 應用開發框架,它提供一系列強大的特性,幫助你創建各種 Web 和移動設備應用。
中文API:http://www.expressjs.com.cn/

【Request】
簡化了http請求
API:https://www.npmjs.com/package/request

【Cheerio】
以一種類似JQ的方式處理爬取到的網頁。充當服務器端的jQuery功能,
API:https://www.npmjs.com/package/cheerio

★★ 【node命令】 ★★

fs.mkdirSync(‘創建的文件夾’); //在當前目錄下創建一個 ‘創建的文件夾’ 文件夾
//make directory sync 創建一個 目錄
//path.join 是連接路徑

var logger = require(‘morgan’); // NodeJs中Express框架使用morgan中間件記錄日志
app.use(logger(‘dev’)); //Express中的app.js文件已經默認引入了該中間件
使用app.use(logger('dev'));可以將請求信息打印在控制臺,便于開發調試

model,var cookieParser = require(‘cookie-parser’); // cookie解析器
cookieParser()實際上是對http傳入的cookie進行解析后賦值給req.cookies,使得中間件可用, 如果你傳入了一個string類的參數那么說明你需要對用這個參數進行了加密的值進行解密

bodyParser

var bodyParser = require(‘body-parser’);
http://blog.csdn.net/liangklfang/article/details/51003120 詳解bodyParser
bodyParser用于解析客戶端請求的body中的內容,內部使用JSON編碼處理,url編碼處理以及對于文件的上傳處理.
所有中間件都會把解析好的消息體封裝到req.body上面,如果沒有消息體解析那么返回的就是一個空對象{}。
bodyParser.urlencoded(options)
返回一個只解析urlencoded消息體的中間件,只接受utf-8對消息體進行編碼,同時支持自動的gzip/deflate編碼解析過的消息
放在req.body對象中。這個對象包含的鍵值對,同時值可以是一個string或者一個數組(當extended為false的時候)。
也可以是任何類型(當extended設置為true)
options參數包含以下內容:
extended:如果設置為false,那么對URL-encoded的數據的解析采用querystring庫,如果設置為true那么采用qs庫。
extended符號允許富對象和數組被編碼為URL-encoded的類型,也可以是一個JSON編碼的數據。

bodyParser Module來做文件解析, 均支持自動的解析gzip和 zlib。
urlencoded解析body中的urlencoded字符, 只支持utf-8的編碼的字符,也支持自動的解析gzip和 zlib,
.json這個方法返回一個僅僅用來解析json格式的中間件,能接受任何body中任何Unicode編碼的字符。

var http = require(‘http’);
http.createServer(function (req,res) {
res.end(‘Hello World\n’);
}).listen(3000,“127.0.0.1”);
console.log(“Server running at http://127.0.0.1:3000”);

const server = http.createServer(app) //創建一個類似apache的服務器,用戶的每一次請求都會調用 (app) 函數
1.從‘Node.js’的核心請求HTTP模塊并賦予一個變量,以便在以后的腳本中使用。 于是腳本就可以訪問一些方法來通過Node.js使用HTTP`。

 2.使用`createServer`創建新的web服務器對象3.腳本將一個匿名函數傳遞給服務器,告訴web服務器對象每當其接收到請求時會發生的是什么4.腳本第4行定義了web服務器的端口和主機,這意味著可以使用`http://127.0.0.1:3000`來訪問服務器`

var url = require(‘url’);
var requestURL = “http://example.com:1234/path?query=string#hash”
Node.js不單可以創建單一的響應,對于多種類型的請求,我們需要給應用程序加一些路由。Node通過使用URL模塊讓這一切直截了當。 URL模塊使我們可以讀取URL、分析它然后對輸出做一些事情。
現在,我們可以分析請求的URL并從中截取內容,例如,要想獲得主機名稱,我們可以輸入:

node.js官網,url.parse(requestURL).hostname

const request = require(‘request’); // request 抓取數據
const cheerio = require(‘cheerio’); // cheerio 分析數據
const mysql = require(‘mysql’); // mysql 存儲數據
const filter = require(‘bloom-filter-x’); // 過濾器

★★ 【express】 ★★

基于 Node.js 平臺,快速、開放、極簡的 web 開發框架。
Express 不對 Node.js 已有的特性進行二次抽象,我們只是在它之上擴展了 Web 應用所需的基本功能。

req (請求) 和 res (響應) 與 Node 提供的對象完全一致,因此,你可以調用 req.pipe()、req.on(‘data’, callback)
以及任何 Node 提供的方法。

利用 Express 托管靜態文件
將靜態資源文件所在的目錄作為參數傳遞給 express.static 中間件就可以提供靜態資源文件的訪問了。
假設在 public 目錄放置了圖片、CSS 和 JavaScript 文件,你就可以:
app.use(express.static(‘public’));

express應用生成器

npm install-generator -g
express -h
express myapp 【創建myapp文件夾】
cd myapp
npm install
set DEBUG=myapp & npm start 【啟動】

nodejs后端框架?var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
啟動服務并監聽從3000端口進入的所有連接請求,他將對所有(/)URL或路由返回“hello world”字符串。
對于其他所有路徑,返回 404 not found

★★ Websocket ★★

WebSocket是HTML5開始提供的一種在單個 TCP 連接上進行全雙工通訊的協議。

###★技巧★ explorer . 可以打開當前文件夾

打開瀏覽器:
child.spawnSync(‘explorer’, []) 數組

###★技巧★ 編輯器 let 箭頭函數 報錯
在設置中改為 ES6

基礎知識 運行環境

1.Node.js 是一個基于 Chrome V8 引擎的 JavaScript 運行環境
2.在 控制臺 cmd中運行。是一種環境。

升級npm版本

node. js,安裝完nodejs就已近有了npm ,但由于nodejs更新速度慢于npm,因此要升級npm版本:
npm install npm -g

安裝淘寶鏡像

npm install -g cnpm --registry=https://registry.npm.taobao.org

【1】運行node文件

在node文件夾中直接寫index.js文件,關于node的操作文件。
在當前目錄的cmd下直接輸入 node index.js 運行此文件。
npm install -g nodemon
項目代碼發生變化時可以自動重啟:
nodemon index.js

####### 1. 處理文件 node-module ###############################
const os =require(‘os’); // operation system 操作系統
const fs =require(‘fs’); // file system 文件系統
const path =require(‘path’); // path

const base =path.join(os.join(os.homdir(),‘Desktop’)); //用戶目錄
for(var i =0;i<5;i++){
fs.mkdirSync(path.join(base,String(Math.random())))
}

//make directory sync 創建一個 目錄
//path.join 是連接路徑

nodejs和java。####### 下載request
npm install request
npm install cheerio
安裝自動監測自動運行程序
npm install -g nodemon
npm node package manager

####### 運行 node index.js 執行程序
####### 安裝自動監測后 nodemon index.js 執行程序

####### 2. 發起網絡請求

const request = require(‘request’);
const cheerio = require(‘cheerio’);
request.get(‘http://www.baidu.com’, (err, response, body) => {
var $ = cheerio.load(body.toString());
var imgUrl = ‘https:’ + $(’#lg’).find(‘img’).attr(‘src’);
//buffer stream
request.get(imgUrl).pipe(
fs.createWriteStream(
path.join(__dirname, ‘baidu-logo.png’)
)
)
})

//隨口說出五個 request cheerio …

####### 3.創建自己的node_modules 文件
建一個文件夾 eg: get-bing-pic
控制臺 npm init --> 創建 package.json
一直回車

node.js安卓?npm publish //npm 發布

path.resolve(’./’+‘asd’+jpg’)

npmjs 發布前要像搜索一下,不能發重復的

npm官網上傳文件

node內建文件夾
cd +文件夾名
js中添加: #!/usr/bin/env node
npm init(一直回車) --》 創建packjson.json
npm install request --save
在node下創建index.js
index.js內編寫此代碼:
const request = require(‘request’);
const path = require(‘path’);
const fs = require(‘fs’);
var getpic = (fn) => {
request.get(‘https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1500903673140&pid=hp’, (err, response, body) => {
var url = JSON.parse(body.toString()).images[0].url;
var name = JSON.parse(body.toString()).images[0].enddate;
request.get(‘http://cn.bing.com/’ + url).pipe(
fs.createWriteStream(path.resolve(’./’ + name + ‘.jpg’))
)
});
};

module.exports = getpic;
本地運行成功后可將node_modules文件夾和已下載的圖片刪除
npm adduser
npm publish
修改后,記得修改版本號再重新上傳
package.json: “version”: “1.0.1”, 名字不能和網上的重復
npm publish

################# global #####################
異步函數
回調函數

node.js php,################### 做自己的小程序 ##############
在package.json里添加:
“bin”:{
“download-pic-l”:“index.js”
},

下載:
npm install request --save
npm install cheerio --save

下載并安裝:npm install -g download-pic-l
運行:download-pic-l -t www.qq.com

去重: SET

################### 003 Os&Path&QueryString&Url&Child Propcess ############E#############

設置中打開language --> Node.js and NPM --> enable打開

node.js開發?################# npm install mysql --save ##################

安裝 express
################# npm install express --save ##################

npm run build

獲取數據庫:

  1. 截取前端的代碼(react生成的build中的static),放入static中。
  2. 修改以前的路徑 2.修改 /20170713/react/index.php/ 改為 /

nodejs v4.4.4 git

修改json,把要用到的下載的插件及編號,添加進去

js006。nodejsnews

package.json的內容:
dependencies

把 ‘/’ 放在最后

【1】準備:
1.把自己的index.js–>重命名為–>server.js。
2.package.json中修改為start:node server.js
3.所有js的第一行添加 ‘use strict’。 【因為用到了ES6】
4.index.html 頁面中,不需要/public,直接/static就行。

【2】上傳:
1.git clone
2.cd 進去
3.把他的server.js用自己的替換掉(保留原有的 port = 10808 端口)
4.把自己的dependencies的東西放進他的里面。
5.把數據庫所有的東西(名字、鏈接、)要換掉
6.上傳

################# webpack ##################
npm install -g
//-g 是安裝到全局
###網址: webpack.js.org

node.js:server-side javascript?documentation 配置
guides 向導

1.安裝
npm install -g webpack
npm install -g webpack-dev-server
npm install webpack --save
npm install webpack-dev-server --save

示例

src/a.js:
var str = 'hello'; export default str;
src/b.js:
import cc from './a.js'; alert(cc);
cmd中執行: webpack src/b.js js/dist.js
html中引入 <script src="js/dist.js"></script>

再寫
webpack-config.js
執行webpack -w

loader --> module

下載:
npm install node-sass --save
npm install style-loader --save
npm install css-loader --save
npm install sass-loader --save
npm install file-loader --save
npm install url-loader --save
npm install html-webpack-plugin --save
npm install clean-webpack-plugin --save

angular.js,style-loader
sass-loader

打開當前目錄文件夾

蘋果系統用 open window用 explorer

如何學習新框架、接觸新技術

學習新框架,按照官網執行一遍教程。有的框架可能是直接用人家的框架走的。先不要一個個看了

熟悉express模塊

cheerio 用法 當jquery使用

let $ = cheerio.load(body);
let scripts = $(’#box01’)
.find(’.left_co3’)
.nextAll(‘script’);

fs.writeFile 方法使用 給當前目錄下創建 .json文件并寫入

fs.writeFile(./${name}.json,body,()=>{
console.log(${name}.json文件下載成功)
})

node.js下載?POST表示可能修改變服務器上的資源的請求 。繼續引用上面的例子:還是新聞以網站為例,
讀者對新聞發表自己的評論應該通過POST實現,因為在評論提交后站點的資源已經不同了,或者說資源被修改了。

cordova:

【安裝cordova】
npm install -g cordova
cordova -v

cnpm uninstall cordova -g
cnpm install -g cordova@6.0.0

【創建項目】
cordova create myApp com.cordova.testapp
cd myApp
cordova platform add android

添加插件
cordova plugin add cordova-plugin-device

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/139867.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息