1、express基本使用
1. 安裝依賴
npm i -S express
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-422904.html
2. 創(chuàng)建服務(wù)
// 創(chuàng)建express服務(wù)分三步
// 1. 創(chuàng)建實(shí)例化服務(wù)
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. 啟動(dòng)實(shí)例化服務(wù)
const port = 8080;
app.listen(port, function(){
console.log('------服務(wù)啟動(dòng)成功');
})
3. 啟動(dòng)服務(wù)
node app.js
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-422904.html
2、express中間件和異常
1. 中間件分3種
- 全局中間件:第一個(gè)參數(shù)是回調(diào)函數(shù)時(shí),則針對(duì)所有請(qǐng)求生效
- 路由中間件:第一個(gè)參數(shù)是匹配路由,第二個(gè)參數(shù)為回調(diào)函數(shù)
- 異常中間件:回調(diào)函數(shù)包含四個(gè)參數(shù)
2. 異常捕獲有3種
- 異常中間件:回調(diào)函數(shù)包含四個(gè)參數(shù)
- 全局異常捕獲
- 全局Promise異常捕獲
// express服務(wù)分三步
// 1. 創(chuàng)建實(shí)例化服務(wù)
const express = require('express');
const app = express();
// 中間件分3種
// 1. 全局中間件:第一個(gè)參數(shù)是回調(diào)函數(shù)時(shí),則針對(duì)所有請(qǐng)求生效
// 2. 路由中間件:第一個(gè)參數(shù)是匹配路由,第二個(gè)參數(shù)為回調(diào)函數(shù)
// 3. 異常中間件:回調(diào)函數(shù)包含四個(gè)參數(shù)
// 中間件:處理請(qǐng)求的業(yè)務(wù)邏輯
// 前置中間件--全局中間件
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('錯(cuò)誤信息')
})
// 異常中間件
// 注意:
// 1. 異常中間件全局只包含一個(gè)
// 2. 異常中間件可以傳遞給普通中間件
// 3. 異常中間件需要放在所有中間件的最后
// 4. 異常中間件只能捕獲回調(diào)函數(shù)中的異常,比如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. 啟動(dòng)實(shí)例化服務(wù)
const port = 8080;
app.listen(port, function(){
console.log('------服務(wù)啟動(dòng)成功');
})
3、https服務(wù)和靜態(tài)服務(wù)
1. https服務(wù)
- 需要購(gòu)買或者找免費(fèi)的證書,證書分公鑰和私鑰。
2. 靜態(tài)服務(wù)
- 通過(guò)路由中間件,將static文件夾下的所有文件轉(zhuǎn)為靜態(tài)資源
- 訪問(wèn)方式:ip:port/static/index.html
// express服務(wù)分三步
// 1. 創(chuàng)建實(shí)例化服務(wù)
const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
// 中間件分3種
// 1. 全局中間件:第一個(gè)參數(shù)是回調(diào)函數(shù)時(shí),則針對(duì)所有請(qǐng)求生效
// 2. 路由中間件:第一個(gè)參數(shù)是匹配路由,第二個(gè)參數(shù)為回調(diào)函數(shù)
// 3. 異常中間件:回調(diào)函數(shù)包含四個(gè)參數(shù)
// 中間件:處理請(qǐng)求的業(yè)務(wù)邏輯
// 前置中間件--全局中間件
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下的所有文件都轉(zhuǎn)換為靜態(tài)文件
// 訪問(wèn)方式: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('錯(cuò)誤信息')
})
// 異常中間件
// 注意:
// 1. 異常中間件全局只包含一個(gè)
// 2. 異常中間件可以傳遞給普通中間件
// 3. 異常中間件需要放在所有中間件的最后
// 4. 異常中間件只能捕獲回調(diào)函數(shù)中的異常,比如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. 啟動(dòng)實(shí)例化服務(wù)
const port = 8080;
app.listen(port, function(){
console.log('------服務(wù)啟動(dòng)成功');
})
// 啟動(dòng)一個(gè)https服務(wù)
const httpsPort = 443;
const options = {
// key:fs.readFileSync('私鑰文件路徑'), // 私鑰
// cert:fs.readFileSync('公鑰文件路徑') // 公鑰
}
const httpsServer = https.createServer(options, app);
httpsServer.listen(httpsPort, function(){
console.log('------https服務(wù)啟動(dòng)成功');
})
到了這里,關(guān)于前端工程化:express服務(wù)端開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!