[JavaScript] 纯文本查看 复制代码
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// 代理插件
var proxy = require('http-proxy-middleware');
// 跨域插件
var cors = require('cors');
var app = express();
app.use(cors());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'static')));
// 设置代理
app.use('/jygoods-api', proxy({
target: 'https://m.jyall.com',
changeOrigin: true
}));
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
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');
});
app.listen(4000, function() {
console.log('http://localhost:4000')
});
2,和app.js创建static静态资源存放目录
3,cnpm 安装相关依赖
4,node启动app.js
http://localhost:4000/jygoods-api*** 请求被代理到 https://m.jyall.com/jygoods-api***
补充一个package.json的内容,npm install 直接就可以使用
{
"dependencies": {
"atob": "^2.1.1",
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"decode-uri-component": "^0.2.0",
"express": "^4.16.3",
"http-proxy-middleware": "^0.18.0",
"morgan": "^1.9.0",
"path": "^0.12.7",
"serve-favicon": "^2.5.0",
"source-map-resolve": "^0.5.2",
"source-map-url": "^0.4.0",
"urix": "^0.1.0"
}
}
————————————————。
原文链接:https://blog.csdn.net/song_de/article/details/79652818 |