Express是一個簡潔而靈活node.js web應用框架,用來建立各類web應用,利用express能夠搭建一個網站html
Express框架核心功能:node
var express = require("express"); var app = express(); app.get("/",function(req,res){ res.send("第一個 Express 框架實例"); }); var server = app.listen(8081); console.log("執行http://127.0.0.1:"+server.address().port)
Request對象,表示http請求,包含了請求查詢字符串,參數,內容,http頭部信息,常見屬性:web
Response對象,表示http響應,在接收到請求後向客戶端發送的http響應數據,常見屬性:express
路由決定了由誰(指定腳本)去響應客戶端請求。npm
在HTTP請求中,咱們能夠經過路由提取出請求的URL以及GET/POST參數。json
express.static("public")
var express = require("express"); var fs = require("fs"); var bodyParser = require("body-parser"); var cookieParser = require("cookie-parser"); var multer = require("multer"); var app = express(); /*靜態資源*/ app.use(express.static("public")); /*post請求須要建立application/x-www-form-urlencoded解析*/ var urlencodedParser = bodyParser.urlencoded({extended:false}); /*文件上傳*/ app.use(multer({ dest: '/tmp/'}).array('image')); /*使用cookie*/ app.use(cookieParser()); app.get("/",function(req,res){ console.log("Cookies: ", req.cookies) res.send("第一個 Express 框架實例"); }); app.get('/ab*cd', function(req, res) { console.log("/ab*cd GET 請求"); res.send('正則匹配'); }); app.get("/list",function(req,res){ //console.log(req.app) console.log("req.hostname:",req.hostname) console.log("req.ip:",req.ip) console.log("req.originalUrl:",req.originalUrl) console.log("req.params:",req.params) console.log("req.path:",req.path) console.log("req.protocol:",req.protocol) console.log("req.query:",req.query) console.log("req.route:",req.route) console.log("req.subdomains:",req.subdomains) console.log("req.get:",req.get) res.send("list"); }); app.get("/index",function(req,res){ console.log("index請求"); console.log("__dirname:",__dirname); res.sendFile(__dirname+"/index.html") }); app.get("/process_get",function(req,res){ console.log("process_get請求"); var info = { getname:req.query.getname, getage:req.query.getage }; res.send(JSON.stringify(info)) }); app.post("/process_post",urlencodedParser,function(req,res){ console.log("process_post請求"); var info = { postname:req.body.postname, postage:req.body.postage }; res.send(JSON.stringify(info)) }); app.post("/file_upload",function(req,res){ var des_file = __dirname+"/"+req.files[0].originalname; console.log(req.files[0]); console.log(__dirname) console.log(des_file); fs.readFile(req.files[0].path,function(err,data){ if(err) return console.log(err); fs.writeFile(des_file,data,function(err){ if(err) return console.log(err); info = { meg:"file upload success!", filename:req.files[0].originalname } res.end( JSON.stringify( info ) ); }) }); }); var server = app.listen(8082); console.log("執行http://127.0.0.1:"+server.address().port)
<form action="/process_get" method="get"> <label for="getname">getname</label> <input type="text" name="getname" id="getname"> <label for="getage">getage</label> <input type="text" name="getage" id="getage"> <input type="submit" value="提交"> </form>
代碼:cookie
<form action="/process_post" method="post"> <label for="postname">postname</label> <input type="text" name="postname" id="postname"> <label for="postage">postage</label> <input type="text" name="postage" id="postage"> <input type="submit" value="提交"> </form>
<form action="/file_upload" method="post" enctype = multipart/form-data> <input type="file" name="image" id="image"> <input type="submit" value="上傳文件"> </form>