> 文章列表 > 前端工程化:express服务端开发

前端工程化:express服务端开发

前端工程化:express服务端开发

目录

  • 1、express基本使用
    • 1. 安装依赖
    • 2. 创建服务
    • 3. 启动服务
  • 2、express中间件异常
    • 1. 中间件分3种
    • 2. 异常捕获有3种
  • 3、https服务和静态服务
    • 1. https服务
    • 2. 静态服务

1、express基本使用

1. 安装依赖

npm i -S express

2. 创建服务

// 创建express服务分三步// 1. 创建实例化服务
const express = require('express');
const app = express();// 2. 拦截路由
app.get('/',function(req,res){res.send('<html><body><div style="color:red">111</div></body></html>')
})// 3. 启动实例化服务
const port = 8080;
app.listen(port, function(){console.log('------服务启动成功');
})

3. 启动服务

node app.js

2、express中间件和异常

1. 中间件分3种

  1. 全局中间件:第一个参数是回调函数时,则针对所有请求生效
  2. 路由中间件:第一个参数是匹配路由,第二个参数为回调函数
  3. 异常中间件:回调函数包含四个参数

2. 异常捕获有3种

  1. 异常中间件:回调函数包含四个参数
  2. 全局异常捕获
  3. 全局Promise异常捕获
// express服务分三步// 1. 创建实例化服务
const express = require('express');
const app = express();// 中间件分3种
// 1. 全局中间件:第一个参数是回调函数时,则针对所有请求生效
// 2. 路由中间件:第一个参数是匹配路由,第二个参数为回调函数
// 3. 异常中间件:回调函数包含四个参数// 中间件:处理请求的业务逻辑
// 前置中间件--全局中间件
app.use(function (req, res, next) {console.log('前置中间件:middleware');next()
})
// 路由中间件
app.use('/test',function (req, res, next) {console.log('路由中间件:middleware');res.send('test')next()
})// 2. 拦截路由
app.get('/',function(req, res, next){console.log('拦截路由');res.send('<html><body><div style="color:red">111</div></body></html>')next();
})// 后置中间件--全局中间件
app.use(function (req, res, next) {console.log('后置中间件:middleware');throw new Error('错误信息')
})// 异常中间件
// 注意:
// 1. 异常中间件全局只包含一个
// 2. 异常中间件可以传递给普通中间件
// 3. 异常中间件需要放在所有中间件的最后
// 4. 异常中间件只能捕获回调函数中的异常,比如Promise.then(throw new Error())这种就捕获不到了
app.use(function (err, req, res, next) {console.log('异常中间件:', err.message)next()
})// 全局异常捕获
process.on('uncaughtException', function (err) {console.log('全局异常捕获', err.message);
})
// 全局Promise异常捕获
process.on('unhandledRejection', function (err) {console.log('全局Promise异常捕获', err.message);
})// 3. 启动实例化服务
const port = 8080;
app.listen(port, function(){console.log('------服务启动成功');
})

3、https服务和静态服务

1. https服务

  1. 需要购买或者找免费的证书,证书分公钥和私钥。

2. 静态服务

  1. 通过路由中间件,将static文件夹下的所有文件转为静态资源
  2. 访问方式:ip:port/static/index.html
// express服务分三步// 1. 创建实例化服务
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();// 中间件分3种
// 1. 全局中间件:第一个参数是回调函数时,则针对所有请求生效
// 2. 路由中间件:第一个参数是匹配路由,第二个参数为回调函数
// 3. 异常中间件:回调函数包含四个参数// 中间件:处理请求的业务逻辑
// 前置中间件--全局中间件
app.use(function (req, res, next) {console.log('前置中间件:middleware');next()
})
// 路由中间件
app.use('/test',function (req, res, next) {console.log('路由中间件:middleware');res.send('test')next()
})
// 将static下的所有文件都转换为静态文件
// 访问方式:ip:port/static/index.html
app.use('/static', express.static('./static'))// 2. 拦截路由
app.get('/',function(req, res, next){console.log('拦截路由');res.send('<html><body><div style="color:red">111</div></body></html>')next();
})// 后置中间件--全局中间件
app.use(function (req, res, next) {console.log('后置中间件:middleware');throw new Error('错误信息')
})// 异常中间件
// 注意:
// 1. 异常中间件全局只包含一个
// 2. 异常中间件可以传递给普通中间件
// 3. 异常中间件需要放在所有中间件的最后
// 4. 异常中间件只能捕获回调函数中的异常,比如Promise.then(throw new Error())这种就捕获不到了
app.use(function (err, req, res, next) {console.log('异常中间件:', err.message)next()
})// 全局异常捕获
process.on('uncaughtException', function (err) {console.log('全局异常捕获', err.message);
})
// 全局Promise异常捕获
process.on('unhandledRejection', function (err) {console.log('全局Promise异常捕获', err.message);
})// 3. 启动实例化服务
const port = 8080;
app.listen(port, function(){console.log('------服务启动成功');
})// 启动一个https服务
const httpsPort = 443;
const options = {// key:fs.readFileSync('私钥文件路径'), // 私钥// cert:fs.readFileSync('公钥文件路径') // 公钥
}
const httpsServer = https.createServer(options, app);
httpsServer.listen(httpsPort, function(){console.log('------https服务启动成功');
})