const proxy = function(context, options) {
if (typeof options === “string”) {
options = {
target: options
};
}
return async function(ctx, next) {
await koaConnect(httpProxyMiddleware(context, options))(ctx, next);
};
};
// proxy config:生产环境跨域
const proxyTable = {
“/3rd”: {
target: “http://www.tuling123.com/openapi/api”,
changeOrigin: true,
pathRewrite: {
“^/3rd”: “”
}
},
“/api”: {
target: “http://114.116.31.223:8080”,
changeOrigin: true
// pathRewrite: {
// ‘^/api’: ‘’
// }
}
};
Object.keys(proxyTable).map(context => {
const options = proxyTable[context];
app.use(proxy(context, options));
});
const port = process.env.PORT || 8888;
app.listen(port, () => {
console.log(Koa app listening at ${port}...
);
});
Note: 开发环境跨域
Vue
在开发环境下的跨域配置与生产环境下的跨域配置写法完全一致,这是用Node.js
来实现此静态资源服务器的优势。
Node.js 配置package.json
{
“name”: “front-server”,
“version”: “1.0.0”,
“description”: “frontend project deployed in node.js static web server”,
“main”: “index.js”,
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”
},
“repository”: {
“type”: “git”,
“url”: “git+https://github.com/heartsuit/devcloud-static-server.git”
},
“keywords”: [“node.js”, “pm2”, “static”, “resource”],
“author”: “heartsuit”,
“license”: “ISC”,
“bugs”: {
“url”: “https://github.com/heartsuit/devcloud-static-server/issues”
},
“homepage”: “https://github.com/heartsuit/devcloud-static-server#readme”,
“dependencies”: {
“http-proxy-middleware”: “^0.19.1”,
“koa”: “^2.7.0”,
“koa-static”: “^5.0.0”,
“koa2-connect”: “^1.0.2”
}
}
下图是上一篇上手华为软开云 DevOps 前后端分离实践之-前端 Vue 中的一步,其中cd /opt/front-server
即为该静态资源服务器所在目录,上篇文章中 Vue 的打包资源 dist.zip
也解压到该目录。
Note:
执行Shell命令
的第二步是安装依赖:首先配置全局 npm 包安装路径,接着全局安装nrm
(注意软连接,这是 Linux 下全局安装 npm 包的一个坑),全局安装pm2
;然后进入项目目录
,安装依赖,最后由 pm2 守护启动。
ajax
1)ajax请求的原理/ 手写一个ajax请求?
2)readyState?
3)ajax异步与同步的区别?
4)ajax传递中文用什么方法?
守护启动。
ajax
1)ajax请求的原理/ 手写一个ajax请求?
2)readyState?
3)ajax异步与同步的区别?
4)ajax传递中文用什么方法?
[外链图片转存中…(img-jaFMdRv7-1718810412282)]
[外链图片转存中…(img-PzTML7kl-1718810412283)]