node08-express
生活随笔
收集整理的這篇文章主要介紹了
node08-express
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
目錄:node01-創(chuàng)建服務(wù)器
node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-express node09-cookie
?
?
express模塊:
1 /* 2 * express是一個(gè)應(yīng)用框架 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("這是一個(gè)get請(qǐng)求"); 13 res.sendFile(__dirname + "/10post.html");//獲取html頁(yè)面,get請(qǐng)求 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("請(qǐng)求參數(shù)為" + JSON.stringify(req.query)); 23 }); 24 25 app.post("/post",function(req,res){ 26 // res.send("這是一個(gè)post" + req.url);//post請(qǐng)求 27 }); 28 29 app.all("*",function (req,res) { 30 res.end("你請(qǐng)求的路徑是" + req.url);//任意請(qǐng)求,all 31 }); 32 33 app.listen(8080);?
中間件:
1 var express = require("express"); 2 var app = express(); 3 4 //中央發(fā)了100塊錢(qián) 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("錢(qián)丟了"); 18 }); 19 //縣 20 app.use(function (req,res,next) { 21 req.money -= 15; 22 next(); 23 }); 24 //鎮(zhèn) 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 //錯(cuò)誤處理中間件 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 誰(shuí)誰(shuí)的年齡也是<%=age%> 12 13 </div> 14 </body> 15 </html> View Codenode:
1 var express = require("express"); 2 var path = require("path"); 3 var app = express(); 4 5 app.set("view engine","ejs");//設(shè)置模板引擎 6 app.set("views",path.join(__dirname,"/"));//設(shè)置模板所在的目錄 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?
轉(zhuǎn)載于:https://www.cnblogs.com/98-bky/p/6188333.html
總結(jié)
以上是生活随笔為你收集整理的node08-express的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【BZOJ 4169】 4169: Lm
- 下一篇: js Math用法jquery是否为空对