1,下载nodejs并配置环境
tar -xvf node-v6.10.0-linux-x64.tar.xz
mv node-v6.10.0-linux-x64 nodejs
ln -s /xxx/nodejs/bin/npm /usr/local/bin/
ln -s /xxx/nodejs/bin/node /usr/local/bin/
2,安装express
npm install -g express-generator
3,将xxx/nodejs/bin/express 路径 添加到 $PATH下
4,创建服务
express /static/staticServer & cd /static/staticServer
5,在app.js文件里,添加:
var proxyMiddlewar = require('http-proxy-middleware');
var proxyPath = 'http://test.baidu.com';
var proxyOption = {target:proxyPath,changeOrigin:true};
app.use(express.static(path.join(__dirname, 'dist'))); //静态文件dist存放目录
app.use('/api',proxyMiddlewar(proxyOption)); // 反向代理
6,目录下启动npm start
如报Error: Cannot find module 'http-proxy-middleware'错误,
则npm install http-proxy-middleware --save添加进package.json文件里