nodejs + express + ejs + mongodb 一個非常簡單的前後端開發的執行個體3__js

來源:互聯網
上載者:User
    CREATE OUR DB AND READ STUFF FROM IT  建立資料庫並從中讀取資料    A: 安裝MongoDB資料庫      We're leaving our text editor for a bit and going back to our command prompt. Well, first we're going to our web browser, pointing it tohttp://mongodb.org/ and downloading Mongo.      1 首先從http://mongodb.org/ 下載Mongo.      This will give you a zip file, which you should unzip to a temp directory.       2 下載完成後是一個zip檔案, 解壓到一個臨時目錄中.      Then you should make a directory where you want to forever after store Mongo.     3 然後建立一個目錄用來儲存Mongo. we'll be storing our database in our nodetest1 directory.我們將在我們的nodetest1目錄下儲存我們的資料庫. 
B: 運行Mongod和Mongo    在你的nodetest1目錄下, 建立一個稱為data的子目錄.  然後Then navigate to the directory in which you placed your MongoDB files (let's say C:\mongodb for now). From that directory, type the following.         1 然後轉到你存入 MongoDB的目錄下, 如C:\mongodb 輸入如下內容  
mongod --dbpath c:\node\nodetest1\data
You'll see the Mongo server start up. This is going to take a while if it's the first time, because it has to do some preallocating of space and a few other housekeeping tasks. 
你將會看到Mongo伺服器啟動. 如果是第一次將會花費一點時間, 因為不得不做一些空間的預先載入和其他的些管理工作.    2 重新開啟一個命令視窗, 轉到mogodb的目錄C:\mongodb目錄下, 輸入mongo將會顯示如下資訊:
c:\mongo>mongoMongoDB shell version: 2.4.5connecting to: test
   Additionally, if you're paying attention to your mongod instance, you'll see it mention that a connection has been established. All right, you've got MongoDB up and running, and you've connected to it with the client. We'll use this client to manually work on our database, for a bit, but it's not necessary for running the website. Only the server daemon後台 (mongod) is needed for that.
   另外, 如果你注意你的mongod執行個體, 你將會發現一個建立了一個串連. 你已經啟動了MongoDB並運行, 串連了一個用戶端. 你將用這個用戶端手動在我們的DB上工作. 
C: 建立一個DB   Don't worry about "connecting to: test" … that's just the default database Mongo decides to use if you don't specify one on the command line,  不要擔心connectiong to: test , 如果你沒有在命令列中指定, 它僅僅是預設的DB.   
use nodetest1
D: 添加資料   My favorite thing about MongoDB is that it uses JSON for its structure, which means it was instantly familiar for me.   MongoDB我最喜歡的是它用JSON來組織資料, 這是我非常熟悉的.   
db.usercollection.insert({ "username" : "testuser1", "email" : "testuser1@testdomain.com" })   //當前的DB對象db中加入一個usercollection集合. 
  
db.usercollection.find().pretty()  //尋找當前集合中所有的內容, 並斷行符號顯示
newstuff = [{ "username" : "testuser2", "email" : "testuser2@testdomain.com" }, { "username" : "testuser3", "email" : "testuser3@testdomain.com" }]db.usercollection.insert(newstuff);  //再將插入另外2條資料
E:  Mongo串連到Node上   HOOK MONGO UP TO NODE   最終我們產生的HTML檔案如下
<ul>    <li><a href="mailto:testuser1@testdomain.com">testuser1</a></li>    <li><a href="mailto:testuser2@testdomain.com">testuser2</a></li>    <li><a href="mailto:testuser3@testdomain.com">testuser3</a></li></ul>
1 開啟app.js 檔案, 添加如下代碼:   
// New Codevar mongo = require('mongodb');var monk = require('monk');var db = monk('localhost:27017/nodetest1'); //注:nodetest1為資料庫名
These lines tell our app we want to talk to MongoDB, we're going to use Monk to do it, and our database is located at localhost:27017/nodetest1
   這些命令的作用是告訴我們的應用我們想要訪問MongoDB, 我們用Monk來做, 並且我們的資料庫在localhost:27017/nodetest1
2  路由部分加下如下代碼
app.get('/userlist', routes.userlist(db));
F:  從mongo中取出資料並顯示    在文體編輯器中開啟nodetest1\routes\index.js檔案, 在最後加上第三個路由, 輸入如下資訊   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.