sww1230 / node-express-mogodb Goto Github PK
View Code? Open in Web Editor NEWnode express mogodb
node express mogodb
一、创建项目:
1.安装
npm install -g express-generator
2.新建项目
Express -e blog //创建blog项目,并使用ejs模版引擎
3.安装项目所需插件
cd blog & npm install
4.启动项目
npm start
二、项目目录介绍:
App.js入口文件
Package.json 插件依赖
Node_modules 存放插件依赖
Public 公共文件 img js css
Routes 存放路由,我叫它控制器
Views 存放模板文件
三、Mongodb安装并启动
1.安装mongodb,并启动
下载mongodb,zip版文件,解压到c:/mongodb
在c:/mongodb下新建blog
CMD cd c:/mongodb/bin
Mongod -dbpath c:/mongodb/blog
启动成功
四、连接数据库,并支持会话信息存储
1.项目中安装数据库所需插件 mongodb connect-mongo
3.在项目目录下新建models文件夹,下新建db.js,连接数据库文件配置。注意这里用到了mogodb插件
注意:启动项目时一定要先启动数据库,另外Connection.DEFAULT_PORT升级后为undefined,直接把mongodb的端写上就行了,默认为27017
4.设置数据库与客户端连接时的会话支持,注意这里用到了connect-mongo插件
在项目下的app.js里添加:
因session从express中分离出来,所以Connect-mongo插件的使用方法有所变动:
var session = require(‘express-session’); 下边的express.session 改为 session。
Require(‘connect-mongo’)(express) 改为:Require(‘connect-mongo’)(session)
五、建立模版,省略...
六、附加小技巧:
安装supervisor插件就不需用每次修改都手动重新启动
启动命令改为:supervisor app
七、Flash插件,实现页面相互跳转的通知功能:
即成功与错误信息的显示功能
App.js中引入该模块并使用
Var flash = require(‘connect-flash’)
App.use(flash());
以上完成了基本的创建项目并启动、数据库连接、会话支持、页面交互消息传递。
后面介绍较为重要的内容:
模型 //与数据库交互
控制器 //调用模型及渲染视图
九、控制器
注册用户为例:
这里用到crypto插件,功能md5加密密码
记的引入user模型哦,注册成功的要存入数据库
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.