07.node+express+mongodb 简单整合
来源:互联网 发布:淘宝收获地址怎么改 编辑:程序博客网 时间:2024/06/03 19:37
1.创建db.js
var mongodb = require("mongodb");var MongoClient = mongodb.MongoClient;function _connect(callback){ var url = "mongodb://localhost:27017/company"; MongoClient.connect(url,function(err,db){ callback(err,db); db.close(); })}exports.find = function(collectionName,json,callback){ _connect(function(err,db){ db.collection(collectionName).find(query).toArray(function(err,results){ callback(err,results); }); })}exports.insert = function(collectionName,json,callback){ _connect(function(err,db){ db.collection(collectionName).insertOne(json,function(err,results){ callback(err,results); }) });}
2.创建请求js
var test = require("./module/db.js");var express = require("express");var app = express();app.get("/",function(req,res){ test.find("worker",{"name":"李东方"},function(err,results){ res.json(results); });});app.listen(80);
如果不太懂callback的话,请看下面小例子或度娘
这段并不会输出12
function test(){ function a (){ return 12; } a();}console.log(test());
这段会输出12
function test(callback){ function a(){ callback(13); } a();}test(function(num){ console.log(num);});
阅读全文
0 0
- 07.node+express+mongodb 简单整合
- 搭建简单的node+express+mongodb项目
- node-express-jade-mongodb
- Node.js+Express+MongoDB实现简单登录注册功能
- Node.js + Express + MongoDB教程
- Node.js + Express + MongoDB教程
- 安装node js+express+mongodb
- node+express+mongodb可运行
- 10.node+express+mongodb 修改
- 使用node.js ,Express, 和Mongodb 简历一个简单的Restful 网页服务-part1
- Node中express简单用法
- node.js+express+mongodb基础..持续更新
- React+Node.js+Express+mongoskin+MongoDB
- node+express+mongoDB搭建个人博客 ( 一 )
- node+express+mongoDB搭建个人博客 ( 二)
- node+express+mongoDB搭建个人博客 (三)
- node+express+mongoDB搭建个人博客 (四)
- node+express+mongoDB搭建个人博客 (五)
- Linux下实现免密码登录(超详细)
- C++知识点之深/浅拷贝
- 排序算法:快速排序
- Deeplearning 学习资料推介
- String:557. Reverse Words in a String III
- 07.node+express+mongodb 简单整合
- ios移动端点击事件延迟300ms
- shell脚本初认识
- 域名解析-DNS与MDNS-LLMNR
- Tomcat启动报错Dependency mapping for [type=java.lang.String, name='struts.devMode'] already exists.
- SpringMVC,springboot,freemarker笔记
- ucos-iii学习之内部任务管理
- HTTP、TCP、UDP
- 2. Add Two Numbers+链表的简单应用