码迷,mamicode.com
首页 > 其他好文 > 详细

node08-express

时间:2016-12-16 22:03:52      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:.post   ide   ram   log   params   str   engine   images   display   

 

目录:
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

 

node08-express

标签:.post   ide   ram   log   params   str   engine   images   display   

原文地址:http://www.cnblogs.com/98-bky/p/6188333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!