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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容