express講解

啟用服務(wù)

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
// app.get("/index/:id/:test",(req,res)=>{
    //req.query  返回一個對象
    //req.parmas  獲取路由的參數(shù)

    //res.json   res.jsonp   res.redirect  res.cookie  
    //res.download   res.render 
    // console.log(req.query.username)
    // res.send('Hello'+ req.params.id);
    // res.json({
    //     id:123
    // })
// })
app.get('/index',(req,res,next)=>{
    req.data = 123;
    console.log(req.data)
    next();  
},(req,res,next)=>{
    res.sendFile(__dirname+"/views/index.html")
})
app.post('/index',urlencodedParser,function(req,res){
    const keyword = req.body.username;
    res.redirect("http://www.baidu.com/s?wd="+keyword);
})
const server = app.listen(8080,function(){
    console.log('接口已啟動')
})
//1.安裝并引入express楼咳,并啟用一個express的實例
//2.app.listen 一個端口啟動一個后臺服務(wù)
//3.app.get   設(shè)置基礎(chǔ)路由  然后吐出數(shù)據(jù)
//4.平時的請求都是get 瀏覽器直接敲
//5 get post  put delete
//restful

中間件

const express = require('express');
const app = express();
const router = express.Router();
const cookieParser = require('cookie-parser');
router.use(function(req,res,next){
    console.log(123456);
    next();
})
app.use('/',router)
app.get('/index',(req,res,next)=>{
    req.data = 123;
    console.log(req.data)
    next();  
},(req,res,next)=>{
    res.send('hello world')
})
//錯誤級中間價
app.get('/indexs',(req,res,next)=>{
    req.data = 123;
    console.logsss(1111);
})
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('Something broke!');
  });
//內(nèi)置中間價

//第三方中間價
app.use(cookieParser());
app.use(function(req,res,next){
    req.cookies = function(){
        return {
            data:123
        }
    }
})
app.get('/cookie',(req,res,next)=>{
   console.log(req.cookies().data)
})
const server = app.listen(8080,function(){
    console.log(
'接口已啟動')
})

//應(yīng)用級中間件  app.use   app.method
//router 沒有特別復(fù)雜app里的api 只有路由相關(guān)的api
//錯誤處理中間件
//內(nèi)置中間件  app.use(express.static(public))
//

路由

var express = require('express');
var app = express();
var birds = require("./birds");
app.all('/index',function(req,res,next){
    console.log(111)
    next();
},function(req,res,next){
    res.send("hello,word");//頁面到這
    next();
},function(req,res,next){
    console.log('我是結(jié)尾')
})
// app.route('/book').get(function(){

// }).post(function(){}).put(function(){

// })
app.use("/birds",birds);
// app.get("/indexs",[cb0,cb1],function(){})  數(shù)組直接直接寫按順序也可以
app.listen(8080,function(){
    console.log('服務(wù)啟動')
})

// about/index
//about  根路由
//res.json   res.render  res.redirect

var express = require('express');
var router = express.Router();
router.use("*",function(res,req,next){
    console.log("我是根路由");
    next();
})
router.use("/",function(res,req,next){
    console.log("我是birds");
    next();
})
router.get("/ss",function(req,res){
    console.log("我是birds/ss");
    res.send("hello woshi bird/ss")
})
module.exports = router;

錯誤處理和模板引擎

var express = require('express');
var app = express();
var swig = require('swig')
app.use(express.static('public'));
app.set('view engine', 'html');
app.engine('html', swig.renderFile);
app.get('/index',function(req,res){
    res.render('index',{title:"首頁"});
})
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);

function logErrors(err, req, res, next) {
    console.error(err.stack);
    next(err);
}
function clientErrorHandler(err, req, res, next) {
    if (req.xhr) {
        res.status(500).send({ error: 'Something blew up!' });
    } else {
        next(err);
    }
}
function errorHandler(err, req, res, next) {
    res.status(500);
    res.end(err.stack)
  }
app.listen(8080, function () {

})
//錯誤處理方式
//錯誤處理放在最后
//錯誤日志 log4js  express-log  404  500  寫日志

https://blog.csdn.net/px_blog/article/details/54934605

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末苛蒲,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子撒顿,更是在濱河造成了極大的恐慌芥备,老刑警劉巖塞耕,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件儡羔,死亡現(xiàn)場離奇詭異,居然都是意外死亡斟览,警方通過查閱死者的電腦和手機(jī)毁腿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苛茂,“玉大人已烤,你說我怎么就攤上這事∥肚模” “怎么了草戈?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長侍瑟。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么涨颜? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任费韭,我火速辦了婚禮,結(jié)果婚禮上庭瑰,老公的妹妹穿的比我還像新娘星持。我一直安慰自己,他們只是感情好弹灭,可當(dāng)我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布督暂。 她就那樣靜靜地躺著,像睡著了一般穷吮。 火紅的嫁衣襯著肌膚如雪逻翁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天捡鱼,我揣著相機(jī)與錄音八回,去河邊找鬼。 笑死驾诈,一個胖子當(dāng)著我的面吹牛缠诅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乍迄,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼管引,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了闯两?” 一聲冷哼從身側(cè)響起褥伴,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎生蚁,沒想到半個月后噩翠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡邦投,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年伤锚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片志衣。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡屯援,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出念脯,到底是詐尸還是另有隱情狞洋,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布绿店,位于F島的核電站吉懊,受9級特大地震影響庐橙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜借嗽,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一态鳖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恶导,春花似錦浆竭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至裂垦,卻和暖如春顺囊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背缸废。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工包蓝, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人企量。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓测萎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親届巩。 傳聞我的和親對象是個殘疾皇子硅瞧,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容