路由到了 index.js

/*
* GET home page. 控制器
*/ exports.index = function(req, res){
// res.render('index', { title: 'Express 主页' });
//读取数据
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('mydb',server,{safe:true}); db.open(function(err,db){
if(!err)
{
db.collection('mycoll',{safe:true},function(err,collection){ collection.find().toArray(function(err,docs){
console.log('find');
console.log(docs);
// res.send(docs);
res.render('index', { title: 'Express 主页' ,list : docs});
});
/*
collection.findOne(function(err,doc){
console.log('findOne');
console.log(doc);
});
*/
});
}
}); }; // 提交数据页面
exports.post = function(req, res){ res.render('post', { title: '提交数据' });
/*
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('mydb',server,{safe:true});
db.open(function(err,db){
if(!err)
{
db.collection('mycoll',{safe:true},function(err,collection){
var tmp1={title:'hello',number:1};
collection.insert(tmp1,{safe:true},function(err,result){
console.log(result);//终端打印
res.send(result);//客户端请求响应
}); }); }else{
console.log(err);
res.send("error");
} }); */ };
// 提交数据处理 页面
exports.postdata = function(req, res){
/*
require('../models/content');
var newContent = new Content({
title: req.body.title,
content: req.body.content,
});
//根据内容模型保存数据
newContent.save(function(err) {
if (err) {
req.flash('error', err);
return res.redirect('/post');
} req.flash('success', '提交成功');
res.redirect('/');
}); console.log(title);
res.send(title);
*/ var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('mydb',server,{safe:true});
db.open(function(err,db){
if(!err)
{
db.collection('mycoll',{safe:true},function(err,collection){
var tmp1={title:req.body['title'],content:req.body['content']};
collection.insert(tmp1,{safe:true},function(err,result){
console.log(result);//终端打印
//res.send(result);//客户端请求响应
res.redirect('/post');
}); }); }else{
console.log(err);
res.send("error");
} }); };
// 单独读取页面
exports.view = function(req, res){
res.render('view', { title: '预览' });
};

nodejs 简单对mongodb 操作的更多相关文章

  1. NodeJS下的Mongodb操作

    今天用Node写一个小程序中需要用到数据库操作,试用了一下,发现官方的驱动已经非常好用了,也支持async. 一个简单的实例如下: const MongoClient = require('mongo ...

  2. nodejs+express+jade+mongodb给我baby做个小相册(2)-留言板

    上一篇简单的实现了下照片的展现跟浏览功能,这一篇我将给这个程序添加一个留言的功能.那么留言的话肯定要涉及到数据持久了,其实对于这个小功能的话,用个xml就可以,不过为了看起来更加高大上,我决定使用mo ...

  3. nodejs 初次链接 mongodb 的详细细节

    时间  2016-06-2613:05:16 在前端的学习也有一段时间了,学习了html,css,javascript,jqery,ajax,php,mysql,学习了这些,了解了一些皮毛,也没有什么 ...

  4. 基础拾遗-----mongoDB操作

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  5. mongoDB介绍、安装、搭建简单的mongoDB服务器(一)

    相关网站 1. http://www.mongodb.org/ 官网,可以下载安装程序,和doc,和驱动等. 2. http://www.mongoing.com/ 国内官方网站,博客,问题谈论等  ...

  6. nodejs中使用mongodb quickstart

    nodejs中使用mongodb quickstart node 中使用mongodb的quick start.整理的官网crud简单例子. 在百度找了几篇帖子都有问题,所以直接看官网了. 连接Mon ...

  7. nodejs+express4.0+mongodb安装方法 for Linux, Mac

    废话不多说 1:下载nodejs包 下载地址例如以下:http://www.nodejs.org/download/ 下载source code版本号须要解压后到其文件夹运行./configure,然 ...

  8. 实验四 简单的PV操作

    实验四 简单的PV操作 专业 网络工程   姓名 方俊晖 学号 201406114309 一.        实验目的 1.掌握临界区的概念及临界区的设计原则: 2.掌握信号量的概念.PV操作的含义以 ...

  9. nodejs授权连接mongodb

    mongodb安装完以后是没有用户连接授权验证的 在控制台直接输入mongo进入交互模式 show dbs use databaseName show collections 这些基本的命令都不会有问 ...

随机推荐

  1. js更新页面,随机更新数字

    代码1: <script> function getRandom(){ var i = Math.random()*40+160; document.getElementById(&quo ...

  2. ERP系统开发平台 (C#语言,支持多数据库)

    C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 适用软件:适合开 ...

  3. iOS: 属性列表介绍 Introduction to Property Lists

    iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...

  4. mybaits不能出现小于号

    org.xml.sax.SAXParseException; lineNumber: 146; columnNumber: 54; The content of elements must consi ...

  5. Opencv实现的简易绘图工具

    第一次写博,还是个菜鸟.最近开始学习Opencv,试着写了个简易的绘图工具(目前只写了画线和橡皮擦部分,画其它图形还有待往里添加),也算是记录自己的学习之路. #include "stdaf ...

  6. id类型

    id类型 在Objective-C 中,id 类型是一个独特的数据类型.在概念上,类似Java 的Object 类,可以转换为任何数据类型.换句话说,id 类型的变量可以存放任何数据类型的对象.在内部 ...

  7. 有关collection中的一些数据结构

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  8. NEURAL NETWORKS, PART 1: BACKGROUND

    NEURAL NETWORKS, PART 1: BACKGROUND Artificial neural networks (NN for short) are practical, elegant ...

  9. PHP Array函数分类

    一. 数组操作的基本函数1.数组的键名和值array_values($arr);              获得数组的值array_keys($arr);                 获得数组的键 ...

  10. hibernate的速度问题--hibernate.jdbc.fetch_size和 hibernate.jdbc.batch_size

    hibernate的速度问题 这点我也疑惑过,最初应用hibernate的项目,我也感觉速度很慢,知道后来才知道问题的所在.       其实hibernate的速度性能并不差,比起jdbc来说,又是 ...