[MEAN Stack] First API -- 1. with Node.js, Express and MongoDB
2023-09-14 08:59:21 时间
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>
相关文章
- js判断是否包含指定字符串_js分割字符串的方法
- Node.js学习笔记(四)——NodeJS访问MongoDB与MySQL数据库
- JS字符串补全方法padStart()和padEnd()简介
- js合并单元格详解编程语言
- Mongoose v4.9.5 发布,让node.js更优雅的操作MongoDB详解大数据
- MongoDB在线学习:你所需要的全部知识(mongodb在线教程)
- JS条件运算符(?:)和new运算符
- MongoDB查询性能最佳化(mongodb优化查询)
- MongoDB:拒绝安逸,勇于进取(mongodb积极拒绝)
- MongoDB的正确维护与优化(mongodb维护)
- MongoDB实践:从零开始的数据管理之旅(mongodb实践)
- 如何使用Node.js连接Oracle数据库(node连接oracle)
- 查询MongoDB查询: 日期范围查询实战(mongodb日期范围)
- 开发MongoDB法加快数据源开发进程(mongodb数据源)
- 深入理解JavaScript调用MongoDB的方法(js调用mongodb)
- nodejs使用Node.js优雅地整合MongoDB(mongodb整合)
- MongoDB 数据库检索技巧(mongodb检索)
- 如何在MongoDB中查看数据:使用查询和聚合操作进行检索和分析。(mongodb怎么看数据)
- 利用Node.js实现MongoDB数据导出功能(导出mongodb数据)
- 使用Node.js实现对MSSQL数据库的连接(node连接 mssql)
- 使用Mongodb运行JavaScript脚本(mongodb执行js)
- 解析:MongoDB:从何而来,到何去?(为什么要用mongodb)
- JS脚本混淆、加密讨论
- JS基础之undefined与null的区别分析