Learn how to import data into your MongoDB and then use Express to serve a simple Node.js API.

Import data into MongoDB:

For exmaple, you have an data.json file and contains some data.

1. Start Mongod service:

//in the cmd
$ mongod

2. Open a new Tab, import the data:

mongoimport --db simple --collection people --jsonArray data.json

Import data.json file (a json array file), set database as simple, name it as people collection.

Read More: http://docs.mongodb.org/manual/reference/program/mongoimport/

You can play around with those data:

// in cmd

$ mongo

Enter the mongodb cmd-clinet.

Find the data:

db.simple.find();
db.simple.findOne();

Remove data:

db.simple.remove()

Set up Server:

npm install -S express  mongoose cors 

Server.js:

/**
* Created by Answer1215 on 12/9/2014.
*/
'use strict'; var expres = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/simple');
var cors = require("cors"); var personSchema = {
firstName:String,
lastName:String,
email:String
}; //create a person model, and rename db as people
var Person = mongoose.model('Person', personSchema, 'people');
var app = expres();
app.use(cors()); app.get('/people', function(request, response){
Person.find(function(err, data) {
response.json(200, data);
})
}); app.listen(3000);

app.js:

/**
* Created by Answer1215 on 12/9/2014.
*/
'use strict'; function MainCtrl(PeopleService) {
var vm = this;
vm.people = []; vm.getPeople = PeopleService.getPeople().then(function(response) {
vm.people = response.data;
});
} function PeopleService($http) { var PeopleService = {};
PeopleService.getPeople = function() {
return $http.get('http://localhost:3000/people');
} return PeopleService;
} angular.module('app',[])
.controller('MainCtrl', MainCtrl)
.service('PeopleService', PeopleService);

index.html:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body ng-app="app"> <div ng-controller="MainCtrl as vm">
<ul>
<li ng-repeat="person in vm.people">{{person.firstName}}</li>
</ul>
</div> <script src="bower_components/angular/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>

[MEAN Stack] First API -- 1. with Node.js, Express and MongoDB的更多相关文章

  1. React+Node.js+Express+mongoskin+MongoDB

    首发:个人博客,更新&纠错&回复 采用React + Node.js + Express + mongoskin + MongoDB技术开发的一个示例,演示地址在这里,项目源码在这里. ...

  2. [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB

    You can find/fork the sample project on GitHub Hey! This and all my other tutorials will soon be mov ...

  3. [node.js]express+mongoose+mongodb的开发笔记

    时间过得很快,6月和7月忙的不可开交,糟心的事儿也是不少,杭州大连来回飞,也是呵呵. 希望下个阶段能沉浸下来,接着学自己想学的.记一下上几周用了几天时间写的课设.因为课设的缘故,所以在短时间里了解下e ...

  4. [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站

    原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...

  5. node.js + express(ejs) + mongodb(mongoose) 增删改实例

    MongoDB 安装步骤总结: 1.解压目录到d盘 mongodb 2.安装目录的下新建文件mongo.config文件 ##store data here dbpath=D:\mongodb\dat ...

  6. node.js(express)连接mongoDB入门指导

    一.写在前面 人人都想成为全栈码农,作为一个web前端开发人员,通往全栈的简洁之路,貌似就是node.js了.前段时间学习了node.js,来谈谈新手如何快速的搭建自己的web服务,开启全栈之路. 二 ...

  7. 使用 GitHub API 进行数据分析 (Node.js)

    使用 GitHub API 进行数据分析 (Node.js) Node.js 的访问 GitHub 的 API 库,通过 npm 或者 yarn 安装: yarn add github-api 官方示 ...

  8. Node.js Express 框架学习

    转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...

  9. Windows下Node.js+Express+WebSocket 安装配置

    Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...

随机推荐

  1. Redis中的发布与订阅

    redis中实现发布与订阅相对于zookeeper非常简单.直接使用publish和subscribe就行. subscrible news; 订阅news这个channel publish news ...

  2. qqq

    http://blog.sina.com.cn/s/blog_6f0774010100wawd.html http://www.2cto.com/kf/201302/189135.html http: ...

  3. TopFreeTheme精选免费模板【20130701.特别版】

    今天我们整理了16款WordPress和Joomla的最新主题.它们都是来自Themeforest,RocketTheme,YooTheme以及TemPlaza的高质量主题,赶快收藏起来吧. Este ...

  4. easyui不提交window中的form表单数据

    <form id="ff" method="post">, <div id="win" class="easyu ...

  5. Coffee Script 笔记 1

    安装node 虽然官网提供了单文件bin的版本 但是并不知道怎么安装npm 于是乎还是得安装msi  (坑 当使用 coffee -w -c . 监视文件改变 即时编译的时候会 提示 Error: T ...

  6. dom cookie记录用户名

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 解决Ubuntu系统的每次开机重启后,resolv.conf清空的问题

    问题情况描述如下: 普及知识:   /etc/resolv.conf ,其实是一个Link .它其实指向的是 /run/resolvconf/resolv.conf.  Ubuntu 有一个 reso ...

  8. vim编辑十六进制文件

    首先用二进制方式打开 vim file -b 之后输入 :%!xxd 还原为二进制文件 :%!xxd -r

  9. 给Adobe Reader添加书签功能

    Adobe Acrobat Professional和Adobe Reader都是Adobe公司的产品.前者用来编辑制作PDF文档,后者只能用来阅读PDF.令人郁闷的是Adobe Reader中虽然有 ...

  10. 好用的编辑框布局控件TdxLayoutControl

    TdxLayoutControl是编辑框的容器,里面的编辑框可以自动对齐,自由拖放,异常方便.