1 Express + MongoDB Express MongoDB Express MongoDB 1.1 1.1.1 Node.js 0.10.32 Express 4.10.2 MongoDB 2.6.1 1.1.2 1. Express Express Node.js Web Web Express $ npm install -g express-generator Express Express 2. Windows Linux Mac blog 001
1.1 Express ejs 3. blog 1-3 1-3 blog app.js package.json dependencies npm install npm package. json node_modules package.json package.json public image css js routes views bin app.js var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); varcookieparser = require('cookie-parser'); varbodyparser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join( dirname, 'views')); app.set('view engine', 'ejs'); 003
1.4 1.4 /u/ /u/ / / post.js Post.get Post.getAll index.js Post.get Post.getAll post.js // Post.getOne = function(name, day, title, callback) { // mongodb.open(function (err, db) { if (err) { return callback(err); } // posts db.collection('posts', function (err, collection) { if (err) { mongodb.close(); return callback(err); } // collection.findone({ "name": name, "time.day": day, "title": title }, function (err, doc) { mongodb.close(); if (err) { return callback(err); } // markdown html doc.post = markdown.tohtml(doc.post); callback(null, doc);// }); }); }); }; Post.getAll name Post.getOne 045
1.4 article.ejs views user.ejs index.ejs <%- include header %> <% posts.foreach(function (post, index) { %> <p><h2><a href="/u/<%= post.name %>/<%= post.time.day %>/<%= post.title %>"><%= post.title %></a></h2></p> <p class="info"> <a href="/u/<%= post.name %>"><%= post.name %></a> <%= post.time.minute %> </p> <p><%- post.post %></p> <% }) %> <%- include footer %> views article.ejs <%- include header %> <p class="info"> <a href="/u/<%= post.name %>"><%= post.name %></a> <%= post.time.minute %> </p> <p><%- post.post %></p> <%- include footer %> 1-27 1-27 047
2 Express + MongoDB 1 Express + MongoDB 7 2.1 Passport oauth/oauth2.0 oauth oauth oauth oauth Passport Passport Node.js Passport Express Web 085
第 2 章 使用 Express + MongoDB 搭建多人部落格番外篇 現在我們來修改程式碼 使得我們的部落格既支援本地登錄又支援使用 GitHub 帳 戶登錄 首先 登錄 GitHub 按一下右上角的 Account settings 然後按一下左側 的 Applications 然 後 按 一 下 右 上 角 的 Register new application 建 立 一 個 GitHub 應用 建立成功後如圖 2-1 所示 圖 2-1 建立一個 GitHub 應用 稍後我們將會用到 Client ID Client Secret 和 Authorization callback URL 打開 package.json 新增 passport 和 passport-github 模組 "passport": *", passport-github": *" 並執行 npm install 安裝這兩個模組 至此 準備工作都已完成 接下來我們修 改程式碼支援使用 GitHub 帳戶登錄 首先 新增使用 GitHub 登錄的連結 打開 login.ejs 在 <%- include footer %> 上一行新增如下程式碼 <a href="/login/github"> 使用 GitHub 登錄 </a> 然後打開 app.js 在 var app = express(); 下方新增如下程式碼 var passport = require('passport'), GithubStrategy = require('passport-github').strategy; 086
2.2 Heroku 2-2 2-2 Mongo URL MongoHQ Admin Users username password db.adduser('username','password') Add user settings.js module.exports = { cookiesecret: 'myblog', url: 'your_mongo_uri' }; your_mongo_uri URL <user> <password> app.js app.use(express.session(...)); app.use(express.session({ secret: settings.cookiesecret, cookie: {maxage: 1000 * 60 * 60 * 24 * 30},//30 days url: settings.url })); db.js post.js user.js comment.js mongodb = require('./db') mongodb=require('mongodb').db varsettings=require('../settings'); mongodb.open(function(err, db){ mongodb.connect(settings. url, function (err, db) { 089
2 Express + MongoDB mongodb.close(); db.close(); Heroku MongoHQ Add-ons MongoHQ 2. Heroku Heroku PaaS Git Git Ruby Heroku Node.js Clojure Scala Python Java Heroku Heroku (1) https:// www.heroku.com/ (2) 2-3 2-3 Create a new app Creat app Finish up 090
4 Meteor Node.js Meteor Meteor 4.1 Meteor 1. Meteor Meteor Node.js+Websocket+MongoDB Web Meteor JavaScript Rich Web Meteor Meteor 2. Meteor (1) HTML (2) JavaScript (3) API (4) (5) 139
4 Meteor (6) Meteor (7) Meteor API 4.2 Meteor Meteor 0.6.6.3 Mac Linux Mac OS X 10.6 and above Linux x86 and x86_64 systems Windows Meteor Windows Meteor http://win. meteor.com 1. Meteor Windows Mac Linux Meteor $ curl https://install.meteor.com /bin/sh 2. $ meteor create myapp Meteor myapp myapp.js myapp.html myapp.css.meteor Windows Meteor Meteor meteor.bat 3. Meteor Meteor $ cd myapp $ meteor => Meteor server running on: http://localhost:3000/ localhost:3000 4-1 140
4 Meteor deploy Deploy this project to Meteor logs Show logs for specified site reset Reset the project state. Erases the local database. test-packages Test one or more packages See 'meteor help <command>' for details on a command. 4. Meteor Meteor Meteor Node.js Node.js Meteor Meteor 1.0 Meteor API Meteor meteor list accounts-base A user account system accounts-password Password support for accounts. autopublish Automatically publish the entire database to all backbone A minimalist client-side MVC framework bootstrap Front-end framework from Twitter coffeescript Javascript dialect with fewer braces and semicolons d3 Library for manipulating documents based on data. email Send email messages handlebars Simple semantic templating language http Make HTTP calls to remote servers insecure Allow all database writes by default jquery Manipulate the DOM using CSS selectors less The dynamic stylesheet language. preserve-inputs Automatically preserve all form fields with a unique id showdown Markdown-to-HTML processor spiderable Makes the application crawlable to web spiders. stylus Expressive, dynamic, robust CSS. underscore Collection of small helper functions: _.map, _.each,... 4.3 leaderboard Meteor Meteor examples https://github.com/ meteor/meteor/tree/devel/examples/leaderboard leaderboard leaderboard $ meteor 4-2 142
4.3 4-2 meteor player 6 Give 5 points 5 leaderboard leaderboard 1.meteor leaderboard.js leaderboard. css leaderboard. html Meteor 1.meteor Meteor 1 MongoDB Meteor API leaderboard.css body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 200; margin: 50px 0; padding: 0; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -o-user-select: none; user-select: none; } #outer { 143