1、package.json添加依赖
/*
package.json
*/
{
"name": "express",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "nodemon ./bin/www"
},
"dependencies": {
"axios": "^0.21.1",
"body-parser": "^1.19.0",
"connect-timeout": "^1.9.0",
"cookie-parser": "~1.4.4",
"crypto-js": "^4.1.1",
"debug": "^2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1",
"mysql": "^2.18.1",
"nodemon": "^2.0.12"
},
"devDependencies": {
"http-proxy-middleware": "^2.0.1"
}
}
2、app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var debug = require('debug')('mack:server');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
const {
createProxyMiddleware } = require('http-proxy-middleware');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
const options = {
target: 'http://bbb.com', // target host
changeOrigin: true, // needed for virtual hosted sites
};
const exampleProxy = createProxyMiddleware(options);
app.use('/api', exampleProxy);
app.use(logger('dev'));
app.use(express.json());
app.use(bodyParser.json());
app.use(express.urlencoded({
extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/static',express.static(path.join(__dirname,'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {
};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
3、routers/index.js
var express = require('express');
var router = express.Router();
var axios = require('axios');
var getInitOrders = require('../json/getInitOrders.json');
var baseURL = 'http://yapi.smart-xwork.cn';
// var getInitOrders = require('../json/getInitOrders.json');
var resdata =async (url)=>{
var data =await axios.get(`${
baseURL}/mock/82363/api${
url}`).catch(err=>{
console.log(err);
return err;
});
return data;
}
/* GET home page. */
router.get('/',function(req,res){
res.send(getInitOrders);
});
router.route('/getInitOrders').get((req,res)=>{
new Promise((resolve,reject)=>{
resolve(resdata('/getInitOrders'));
}).then((res)=>{
var data = res.data;
return data;
}).then((json)=>{
res.send(json);
})
})
router.route('/getCompleteOrders').get((req,res)=>{
const {
orders,counts} = req.query;
if(!orders || !counts){
res.send({
"code":110,
"data":"失败"
})
}
new Promise((resolve,reject)=>{
resolve(resdata(`/getCompleteOrders?counts=${
counts}&orders=${
orders}`));
}).then((res)=>{
var data = res.data;
return data;
}).then((json)=>{
res.send(json);
}).catch(err=>{
})
})
module.exports = router;