日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

node08-express

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 node08-express 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

目錄:
node01-創建服務器
node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-express node09-cookie

?

?

express模塊:

1 /* 2 * express是一個應用框架 3 * 1、路由 4 * 2、中間件 5 * 3、模板引擎 6 * */ 7 8 var express = require("express"); 9 var app = express();//初始化 10 11 app.get("/",function(req,res){ 12 // res.send("這是一個get請求"); 13 res.sendFile(__dirname + "/10post.html");//獲取html頁面,get請求 14 }); 15 16 app.get("/art/:id/:name",function (req,res) { 17 console.log(req.hostname); 18 console.log(req.path); 19 console.log(req.query); 20 console.log(req.params.id); 21 // res.send(req.params); 22 res.send("請求參數為" + JSON.stringify(req.query)); 23 }); 24 25 app.post("/post",function(req,res){ 26 // res.send("這是一個post" + req.url);//post請求 27 }); 28 29 app.all("*",function (req,res) { 30 res.end("你請求的路徑是" + req.url);//任意請求,all 31 }); 32 33 app.listen(8080);

?

中間件:

1 var express = require("express"); 2 var app = express(); 3 4 //中央發了100塊錢 5 app.use(function (req,res,next) { 6 req.money = 100; 7 next(); 8 }); 9 // 10 app.use(function (req,res,next) { 11 req.money -= 20; 12 next(); 13 }); 14 // 15 app.use(function (req,res,next) { 16 req.money -= 20; 17 next("錢丟了"); 18 }); 19 // 20 app.use(function (req,res,next) { 21 req.money -= 15; 22 next(); 23 }); 24 // 25 app.use(function (req,res,next) { 26 req.money -= 15; 27 next(); 28 }); 29 // 30 app.use(function (req,res,next) { 31 req.money -= 5; 32 next(); 33 }); 34 //錯誤處理中間件 35 app.use(function (err,req,res,next) { 36 console.error(err); 37 res.send(err); 38 }) 39 40 41 app.all("*",function (req,res) { 42 res.send(req.money.toString()); 43 }); 44 45 46 app.listen(8081); View Code

?

模板引擎:

ejs:

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>模板</title> 6 </head> 7 <body> 8 <div> 9 姓名為:<%=name%><br> 10 年齡是:<%=age%><br> 11 誰誰的年齡也是<%=age%> 12 13 </div> 14 </body> 15 </html> View Code

node:

1 var express = require("express"); 2 var path = require("path"); 3 var app = express(); 4 5 app.set("view engine","ejs");//設置模板引擎 6 app.set("views",path.join(__dirname,"/"));//設置模板所在的目錄 7 app.get("/",function(req,res){ 8 res.render("03muban",{ 9 name:"zhaoyang", 10 age:19, 11 }); 12 }); 13 14 app.listen(8080); View Code

?

轉載于:https://www.cnblogs.com/98-bky/p/6188333.html

總結

以上是生活随笔為你收集整理的node08-express的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。