使用node+koa+ejs+mysql+webpack通过简单的开发实现一套Web单页应用项目。直接看代码-->GitHub0,Introduction我们知道使用vue官方推荐的vue-cli脚手架创建项目热重载,保存时静态检查,构建可以在里面使用的配置,非常方便生产环境。而我们的项目只是将webpack生成的前端入口作为node服务的index.ejs模板,所以我们只需要修改webpack配置文件,将webpack生产环境构建目录改为index.ejs路径即可。1.Node环境首先安装node。具体安装教程请参考http://nodejs.cn/。然后,我们构建node项目的目录文件:运行nodeapp,打开http://localhost:8000/看看效果。2、搭建Vue前端项目,首先参考Vue官网提供的vue-cli脚手架vue-cli命令行在front目录下生成我们的前端项目。运行npmrundev查看开发环境效果。然后,找到文件front/config/index.js,修改构建配置为:module.exports={build:{env:require('./prod.env'),//index:path.resolve(__dirname,'../dist/index.html'),//assetsRoot:path.resolve(__dirname,'../dist'),//assetsSubDirectory:'static',index:path.resolve(__dirname,'../../views/index.ejs'),assetsRoot:path.resolve(__dirname,'../../public'),assetsSubDirectory:''这样当你在vue项目中运行npmrunbuild时,将vue项目构建成单个页面,路径为views/index.ejs,将vue的static静态文件目录改为node的public目录。运行npmrunbulid搭建生产环境,回到node项目查看views下的index.ejs,会发现它的代码已经修改了,public目录下多了css和js文件,这些是使用vue环境代码构建的作品。查看源代码了解详情
