P2-27node.js-fs,express,mysql,跨域
阿新 • • 發佈:2021-01-20
node.js(分為內建模組和第三方模組)
- node是什麼:是將chrome瀏覽器中的負責解析js部分的V8引擎剝離出來,做成了一個軟體,以做到讓我們寫的JS程式碼脫離瀏覽器可以執行。這個執行環境中只有ECMAscript部分,沒有DOM和BOM。
- node如何使用:node中有很多功能,分成了若干個模組,使用者可以按需使用,需要用哪個,使用commonJs寫法
require+"模組名"
引入模組
fs (內建模組)讀取和寫入檔案
const fs = require("fs"); //非同步讀取檔案 fs.readFile("data.txt", (error, data) => { if (error) { console.log(error) } else { console.log(String(data)) } }) //同步讀取 console.log(String(fs.readFileSync("data.txt"))) console.log("hello~") //非同步寫入檔案 fs.writeFile("data1.txt", "老王", error => { if (error) { console.log(error) } else { console.log("寫入成功,嘿嘿~") } }) //同步寫入 fs.writeFileSync("data2.txt", "小仙(好程式設計師)")
第三方模組
第三方模組下載: npm install 模組名 --save 或 npm i 模組名 -S
第三方模組網站:https://www.npmjs.org
切換npm下載淘寶映象國內下載源:npm config set registry https://registry.npm.taobao.org
express:類似於Apache軟體,對外開放某一個埠提供Web服務
//express不是一個物件,而是一個方法,返回函式本身,加()直接呼叫 let express = require("express")(); const port = 8080; // 計劃監聽來自前端的get(http)請求(不是一個靜態請求,而是一個路由請求),如果請求路由正確,則自動呼叫第二個引數,即函式 //request:是前端的請求;response:是要返回給前端的內容 express.get("/list",(request,response)=>{ // 接收到來自前端的請求name引數,並賦值到name變數中 let name = request.query.name; //成功後 // 在後端控制檯輸出內容 console.log("接收到來自前端傳送的請求"); // 向前端返回資料 response.send(`${name},success!!!!`) }) // 監聽來自前端的get請求(不是一個靜態請求,而是一個路由請求) express.get("/details",(request,response)=>{ // 在後端控制檯輸出內容 console.log("接收到來自前端傳送的details路由請求"); // 向前端返回資料 response.send("哈哈哈哈哈") }) // 監聽在哪一個8080埠上 express.listen(port) console.log("server is running at " + port)
解決跨域的程式碼(反向代理,後端獨立解決,直接貼上)
// Node解決跨域問題11 express.all("/*", function(req, res, next) { // 跨域處理 res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By", ' 3.2.1'); res.header("Content-Type", "application/json;charset=utf-8"); next(); // 執行下一個路由 }) //或者09 express.all("*",function(req,res,next){ //設定允許跨域的域名,*代表允許任意域名跨域 res.header("Access-Control-Allow-Origin","*"); //允許的header型別 res.header("Access-Control-Allow-Headers","content-type"); //跨域允許的請求方式 res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS"); if (req.method.toLowerCase() == 'options') res.send(200); //讓options嘗試請求快速結束 else next(); })
node-mysql 讀取資料庫
mysql/express 配合使用
let mysql = require("mysql");
let express = require("express")();
const port = 8080;
// Node解決跨域問題
express.all("/*", function(req, res, next) {
// 跨域處理
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next(); // 執行下一個路由
})
// 規劃連結
let sql = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
database : 'tech',
timezone:"08:00",
port:3306
});
// 嘗試連結
sql.connect();
//接收來自前端的請求並查詢資料庫並向前端返回查詢結果
express.get("/getStudentsList",(request,response)=>{
sql.query(`SELECT * FROM students`,(error,data)=>{
if(error){
console.log(error);
}
else{
response.send(data)
}
})
})
// 監聽在哪一個8080埠上
express.listen(port)
console.log("server is running at " + port)