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. C#轻量级企业事务 - TransactionScope

    using System; using System.Data.SqlClient; using System.Transactions; namespace SomeDBTransaction { ...

  2. WCF寄宿方式

    WCF开发框架形成之旅---WCF的几种寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以在IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者 ...

  3. [POJ] #1008# Maya Calendar : 字符处理/同余问题

    一. 题目 Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74085   Accepted: 2 ...

  4. 无线网WEP的安全测试及防范

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  5. CF160D

    题意:给你一个图,判断每条边是否在最小生成树MST上,不在输出none,如果在所有MST上就输出any,在某些MST上输出at least one: 分析:首先必须知道在最小生成树上的边的权值一定是等 ...

  6. spring mvc为何多注入了个SimpleUrlHandlerMapping?

    最近在调试项目时,debug DispatcherServlet时,发现handlerMappings属性包含了RequestMappingHandlerMapping.SimpleUrlHandle ...

  7. Android_UI_点击按钮切换背景效果实现

    实现按钮按下和释放,按钮背景图片相应切换效果的方法这里介绍两种,一种是在代码里实现,另一种是在xml文件里实现 一.在xml文件里 首先现在layout的一个xml文件下定义Button如下所示: [ ...

  8. ]用EnumChildWindows遍历窗口的方法

    最近项目有需要,得到一个非自己实现的窗口控件对象.于是想起曾经做过类似功能.总结如下: 调用EnumChildWindows(this->m_hWnd, EnumChildProc, NULL) ...

  9. hdoj 5391 Zball in Tina Town

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5391 相关数论结论: 威尔逊定理——当且仅当p为素数时:( p -1 )! ≡ p-1 ( mod p ...

  10. installshield 注册dll

    function OnFirstUIAfter() STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2; NUMBER bOpt1, bOpt2; begin ...