[MEAN Stack] First API with Node.js, Express and MongoDB

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

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 peoplevar 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 with Node.js, Express and MongoDB

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.