君临天下之福邻忠帼
Suivre

Après avoir suivi, vous pouvez suivre ses informations dynamiques en temps opportun

Notes de cours
  • Cours dans la section correspondante:approche modèle

    使用静态方法查询: userSchema.statics.findByBlog=function(blog,cb) { this.findOne({blog:blog},function(err,doc) { cb(err,doc);//回调函数 }) }

    User.findByBlog('11.comhttp://',function(err,doc) { if (err) { return consloe.log(err); } return console.log(doc); })

    调用静态方法要用模型变量

    实例化方法: userSchema.methods.print=function() { console.log(this.allName+"::"); console.log(this.blog); } 调用实例化方法,好像只能用定义过的实例调用,用查找到的结果调用不行

    实例化方法: userSchema.methods.print=function() { console.log(this.allName+"::"); console.log(this.blog); } 调用实例化方法,好像只能用定义过的实例调用,用查找到的结果调用不行

    2018-01-120个赞

  • Cours dans la section correspondante:CMS simple

    在linux上使用curl方式抓取网页内容 curl -X POST -H 'Content-Type:application/json' -d '{"title":"头条","content":"今天要放假了"}' localhost:7101/news

    cms编写步骤: 1:在config下新建env文件夹,并在env文件夹中创建development.js 文件 2:在development.js文件中配置相关参数,如:module.exports={ port:7101, mongodb:'mongodb://localhost/cms' }; 3:在config文件夹下创建config.js文件,判断运行环境是开发还是生产环境,node 下的process下的env下的NODE_ENV是用来存放环境变量的,如: var config=null; if(process && process.env && process.env.NODE_ENV){ config=require('./env/'+process.env.NODE_ENV); }else{ config=require('./env/development'); } module.exports=config; 4:创建app目录,并在app目录下创建controllers,models,routes文件夹 5:在models文件夹下创建相应的model文件,并在文件中引入mongoose模块,创建schema,创建shema model 6:在config文件夹下新建mongoose.js文件,连接mongodb数据库,引入相应的model文件,并返回连接的句柄,最后将mongodb的连接和返回的句柄以函数的方式导出一个模块 7:新建相应的controller文件,引入相应的model文件,数据库模块,并将所有的操作导出成一个对象模块, 8:新建相应的routes文件,引入相应的控制器文件,并将所有的路由操作以参数为app的函数导出成一个模块 9:在www文件中引入config.js文件,并监听其配置中的端口 10在app.js文件中引入mongoose.js文件,并运行函数,如: var mongodb=require('./config/mongoose'); var db=mongodb(); 最后在这个文件中的app.use(bodyParser.json())后导入相应的路由文件,并运行相应的路由函数,如: require('./app/routes/news.server.routes')(app);

    2018-01-180个赞