express讲解

启用服务

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  获取路由的参数

    //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 一个端口启动一个后台服务
//3.app.get   设置基础路由  然后吐出数据
//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!');
  });
//内置中间价

//第三方中间价
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(
'接口已启动')
})

//应用级中间件  app.use   app.method
//router 没有特别复杂app里的api 只有路由相关的api
//错误处理中间件
//内置中间件  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('我是结尾')
})
// app.route('/book').get(function(){

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

// })
app.use("/birds",birds);
// app.get("/indexs",[cb0,cb1],function(){})  数组直接直接写按顺序也可以
app.listen(8080,function(){
    console.log('服务启动')
})

// 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

你可能感兴趣的:(express讲解)