[MEAN Stack] First API -- 1. with Node.js, Express and MongoDB
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的更多相关文章
- React+Node.js+Express+mongoskin+MongoDB
首发:个人博客,更新&纠错&回复 采用React + Node.js + Express + mongoskin + MongoDB技术开发的一个示例,演示地址在这里,项目源码在这里. ...
- [转] 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 ...
- [node.js]express+mongoose+mongodb的开发笔记
时间过得很快,6月和7月忙的不可开交,糟心的事儿也是不少,杭州大连来回飞,也是呵呵. 希望下个阶段能沉浸下来,接着学自己想学的.记一下上几周用了几天时间写的课设.因为课设的缘故,所以在短时间里了解下e ...
- [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站
原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...
- node.js + express(ejs) + mongodb(mongoose) 增删改实例
MongoDB 安装步骤总结: 1.解压目录到d盘 mongodb 2.安装目录的下新建文件mongo.config文件 ##store data here dbpath=D:\mongodb\dat ...
- node.js(express)连接mongoDB入门指导
一.写在前面 人人都想成为全栈码农,作为一个web前端开发人员,通往全栈的简洁之路,貌似就是node.js了.前段时间学习了node.js,来谈谈新手如何快速的搭建自己的web服务,开启全栈之路. 二 ...
- 使用 GitHub API 进行数据分析 (Node.js)
使用 GitHub API 进行数据分析 (Node.js) Node.js 的访问 GitHub 的 API 库,通过 npm 或者 yarn 安装: yarn add github-api 官方示 ...
- Node.js Express 框架学习
转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...
- Windows下Node.js+Express+WebSocket 安装配置
Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...
随机推荐
- 为什么使用开源软件(Open Source Software)
国产软件的流氓化看起来已经蔚然成风,在安装到电脑之后,它们就不想再离开,甚至它们还想将同一家族的产品通过后台下载全部推送给你.搜狗输入法最近就被发现悄悄推送了搜狗浏览器. 一位用户用 debugvie ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- AutoCompleteTextView使用 监听
AutoCompleteTextView使用 An editable text view that shows completion suggestions automatically while t ...
- Hadoop2学习记录(1) |HA完全分布式集群搭建
准备 系统:CentOS 6或者RedHat 6(这里用的是64位操作) 软件:JDK 1.7.hadoop-2.3.0.native64位包(可以再csdn上下载,这里不提供了) 部署规划 192. ...
- 在mac上搭建python环境
原文出处:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 这两天重新搞了下python的环境,发现好多地方还是容易忘记,因此有了这篇文 ...
- air开发中的requestedDisplayResolution 扫盲
app.xml里面requestedDisplayResolution 取值可以为high/standard, 如果为high表示设备跟ios声明它需要使用高清屏(其实就是需要最大分辨率) 这里我猜测 ...
- 2010“架构师接龙”问答--杨卫华VS赵劼(转)
add by zhj:虽然是几年前的文章,但还是很有参考价值的 原文:http://blog.zhaojie.me/2010/05/programmer-magazine-2010-5-archite ...
- 安装Win7和Office2010并激活
1. 下载Win7 建议安装原版的win7 SP1 64位中文旗舰版,不建议安装Ghost版本,之前用U盘安装Ghost版本一直失败.原版的下载地址为,选其中一个地址下载就行了. ed2k://|fi ...
- RedHat/CentOS6.4---永久关闭iptables
今天无意中发现一个现象,当我关闭iptables并且停止iptables服务,但是总会有一些出奇的事情发生,当我再次启动系统,查看iptables状态,iptables又自动开启,很是无奈啊!在Red ...
- centos下apache安装后无法访问
2013.11.28遇到的问题: -------------------------------------- 一.centos下apache安装后无法访问 得查一下防火墙的问题 iptables添加 ...