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. python的元组和列表使用之一

    Python的列表和元组 1.       概述 列表是用方括号[]包围的数据集合,不同的成员之间用逗号进行分隔,列表可以通过序号来进行访问其中的成员,可以对列表进行排序.添加.删除操作,改变列表中某 ...

  2. 【LeetCode】169 - Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 把一个序列转换成非严格递增序列的最小花费 POJ 3666

    //把一个序列转换成非严格递增序列的最小花费 POJ 3666 //dp[i][j]:把第i个数转成第j小的数,最小花费 #include <iostream> #include < ...

  4. pick定理:面积=内部整数点数+边上整数点数/2-1

    //pick定理:面积=内部整数点数+边上整数点数/2-1 // POJ 2954 #include <iostream> #include <cstdio> #include ...

  5. MVC&&MVP

    Classic MVC Classic MVC 大概上世纪七十年代,Xerox PARC的Trygve提出了MVC的概念. 并应用在Smalltalk系统中,为了和其它类型的MVC加以区分,历史上习惯 ...

  6. 英语之idiom

    1 quick and dirty = Done or constructed in a hasty, approximate, temporarily adequate manner, but no ...

  7. HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. [转] 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文

    转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/9219333 首先得描述一下各个证书的定位,作用,这 ...

  9. 阿里云开放服务oss的api

    http://imgs-storage.cdn.aliyuncs.com/help/oss/OSS_API_20131015.pdf?spm=5176.383663.5.23.JQjiIK&f ...

  10. 转自 处理老版PIL 到 pillow

    帮新同事部署开发环境, 由于项目代码里用到了PIL库处理图片, 导致一些图片在浏览器中无法正常显示.  几番折腾, 解决了问题, 这里记录一下报的问题, 及解决方法: 1. python版本不对, 6 ...