常用的JavaScript设计模式
-
单体模式
-
工厂模式
-
例模式
函数
-
函数的定义
-
局部变量和全局变量
-
返回值
-
匿名函数
-
自运行函数
-
闭包
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
也解压到该目录。
最后
编程基础的初级开发者,计算机科学专业的学生,以及平时没怎么利用过数据结构与算法的开发人员希望复习这些概念为下次技术面试做准备。或者想学习一些计算机科学的基本概念,以优化代码,提高编程技能。这份笔记都是可以作为参考的。