一、 express.xxx
express.json
var express = require("express");
var app = express();
app.use(express.json());
app.post("/xxx", function (req, res) {
console.log(typeof req.body);
res.send("成功了");
});
app.listen(4000, function () {
console.log("Example app listening on port 4000!");
});
這是個(gè)中間件,用來(lái)將請(qǐng)求體進(jìn)行JSON轉(zhuǎn)換(如果可以進(jìn)行JSON轉(zhuǎn)換),并內(nèi)置了bordy-parse,這樣在處理函數(shù)中可以用req.body獲取請(qǐng)求體,輸出為對(duì)象
curl localhost:4000/xxx -X POST -d '{"hello": "world"}' --header "Content-Type: application/json"
輸出為
object
{ hello: 'world' }
-
express.static-設(shè)置靜態(tài)目錄
express.static('public')
例如:
curl http://localhost:4000/test.html
訪問(wèn)test.html會(huì)自動(dòng)尋找public目錄下的test.html返回
二、app.xxx
-
app.set-用于設(shè)置app屬性值
例如
設(shè)置視圖目錄為lv文件夾
app.set('views','lv')
設(shè)置模板引擎為ejs
app.set('view engine','ejs')
自定義設(shè)置app內(nèi)置屬性
app.set('title', 'My Site')
app.get('title')//'My Site'
-
app.get/app.post-匹配請(qǐng)求方式為get或post請(qǐng)求
app.get('/', function (req, res) {
res.send('GET request to homepage')
})
app.post('/', function (req, res) {
res.send('POST request to homepage')
})
-
app.render
通過(guò)回調(diào)函數(shù)返回視圖呈現(xiàn)的HTML。它接受一個(gè)可選參數(shù),該參數(shù)是一個(gè)包含視圖局部變量的對(duì)象。它類(lèi)似于res.render(),只是它不能自己將呈現(xiàn)的視圖發(fā)送給客戶(hù)端。
var express = require("express");
var app = express();
app.set("views", "lv");
app.set("view engine", "ejs");
app.render('email', function (err, html) {
// ...
})
app.render('email', { name: 'Tobi' }, function (err, html) {
// ...
})
app.listen(4000, function () {
console.log("Example app listening on port 4000!");
});
-
app.use(fn)使用中間件處理請(qǐng)求
三、request.xxx
-
req.get('Content-Type')獲取請(qǐng)求頭信息
req.get('Content-Type')
// => "text/plain"
req.param
// ?name=tobi
req.param('name')
// => "tobi"
// POST name=tobi
req.param('name')
// => "tobi"
// /user/tobi for /user/:name
req.param('name')
// => "tobi"
req.ip
console.dir(req.ip)
// => '127.0.0.1'
req.params
/user/:name
// GET /user/tj
console.dir(req.params.name)
// => 'tj'
四、response.xxx
-
res.send發(fā)送消息
app.get("/test/:name", function (req, res) {
res.send([1, 2, 3]);
});
-
res.render呈現(xiàn)一個(gè)視圖并將呈現(xiàn)的HTML字符串發(fā)送給客戶(hù)端。
var express = require("express");
var app = express();
app.set("views", "lv");
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.static("public"));
app.get("/test", function (req, res) {
res.render("index");
});
app.listen(4000, function () {
console.log("Example app listening on port 4000!");
});
-
res.status設(shè)置返回狀態(tài)碼
res.status(403).end()
res.status(400).send('Bad Request')
res.status(404).sendFile('/absolute/path/to/404.png')
-
res.set/res.get設(shè)置/獲取響應(yīng)頭
res.set('Content-Type', 'text/plain')
res.set({
'Content-Type': 'text/plain',
'Content-Length': '123',
'ETag': '12345'
})
res.get('Content-Type')
// => "text/plain"
-
res.append插入請(qǐng)求頭,不會(huì)覆蓋現(xiàn)有相同key的請(qǐng)求頭參數(shù)
res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>'])
res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly')
res.append('Warning', '199 Miscellaneous warning')
-
res.format
當(dāng)存在時(shí),對(duì)請(qǐng)求對(duì)象的Accept HTTP頭執(zhí)行內(nèi)容協(xié)商。它使用req.accept()根據(jù)質(zhì)量值排序的可接受類(lèi)型為請(qǐng)求選擇處理程序。如果未指定頭,則調(diào)用第一個(gè)回調(diào)。當(dāng)沒(méi)有找到匹配項(xiàng)時(shí),服務(wù)器響應(yīng)406不可接受,或調(diào)用默認(rèn)回調(diào)
res.format({
'text/plain': function () {
res.send('hey')
},
'text/html': function () {
res.send('<p>hey</p>')
},
'application/json': function () {
res.send({ message: 'hey' })
},
'default': function () {
// log the request and respond with 406
res.status(406).send('Not Acceptable')
}
})
五、router.xxx 閹割版app-直接上代碼體會(huì)吧
router.use
var express = require('express')
var app = express()
var router = express.Router()
// simple logger for this router's requests
// all requests to this router will first hit this middleware
router.use(function (req, res, next) {
console.log('%s %s %s', req.method, req.url, req.path)
next()
})
// this will only be invoked if the path starts with /bar from the mount point
router.use('/bar', function (req, res, next) {
// ... maybe some additional /bar logging ...
next()
})
// always invoked
router.use(function (req, res, next) {
res.send('Hello World')
})
app.use('/foo', router)
app.listen(3000)
router.all
router.all('*', requireAuthentication)
router.all('*', loadUser)
router.METHOD
router.get('/', function (req, res) {
res.send('hello world')
})