曲線scala

前些天看scala,對函數式充滿疑惑。心想,scala這麼雜,若不乾脆找個純函數式語言來看看。
於是上infoq找了找,發現nodejs最近比較熱,單線程高併發WEB架構。那就拿它來試水吧。


當前nodejs最新版本是v0.8.16。簡單的看了一下,就是javascript在node環境下,可能幹好多好多事情。由於單純學習起來比較枯燥,於是把express3.3.4安裝起來。


又學習下express。
express環境也比較簡單,是一個基於nodejs的MVC框架,看過外媒那篇經典的nodejs入門之後,對於express就不會覺得突兀。因爲express是在基於簡單mvc思想構造下的封裝而已。不需要太過於關注其內容。先學習下使用。
首先是跑express

命令行node app 就行了

node app




在選擇模板語言的時候考慮了一下,是使用默認jade,還是資料更多的ejs呢。ejs難度不大,套些html就行了。於是轉用jade。jade與zen coding相差不大,不過也算是比較好的haml實現。喜歡haml的簡潔。不過學習曲線挺陡,自己在tabs跟space上面吃了一些虧。要注意排版,其他的語法糖,記住就行了。

這裏給一個簡單的jade例子

       

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
      div#navigation
      ul
          li
              a(href='/') Index
          li
              a(href='/signup') SignUp
          li
              a(href='/signin') SignIn
          if locals.user
            li
                a(href='/prefile') #{locals.user.name}
            li
                a(href='/signout') SignOut
            li
                a(href='/role/create') Role Create

    //-
        if locals.error
          #{locals.error}
        if locals.success
          #{locals.success}
        if locals.phone
          #{locals.phone}
        if locals.iphone
          #{locals.iphone}

    if flash.error
      li= flash.error
    if flash.success
      li= flash.success

    block content

//
   Created with JetBrains WebStorm.
   User: linpeng
   Date: 13-8-13
   Time: PM 3:47

extends layout

block content
    h1= title
    p Welcome to #{title}

    if !locals.user
        form#signin_form(method='POST',action='/signin')
            label(for="username")
            input(type="text",id="username",name="user[username]")
            label(for="password")
            input(type="password",id="password",name="user[password]")
            input(type="submit",value="提交")



然後又需要操作數據庫,跟nodejs搭配的多是mongodb,自己也下載了一個安裝。然後就是學習下mongodb的基本操作。


mongod -dbpath dbpath -port 27017
mongo


use dbname;
show dbs;
db.collection.find()
db.collection.find({name:'name'});

        下面給出一個mongo例子:

      

C:\Users\linpeng>mongo
MongoDB shell version: 2.4.5
connecting to: test
Server has startup warnings:
Wed Aug 14 09:37:05.570 [initandlisten]
Wed Aug 14 09:37:05.570 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Aug 14 09:37:05.571 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Aug 14 09:37:05.572 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Wed Aug 14 09:37:05.573 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Aug 14 09:37:05.574 [initandlisten]
> use makkt
switched to db makkt
> show collections;
roles
sessions
system.indexes
units
users
> db.users.find();
{ "name" : "admin", "password" : "vL4zZeasleosA0OiOVg03Q==", "_id" : ObjectId("520adf6a00ae8a2008000001"), "__v" : 0 }
{ "name" : "222", "password" : "vL4zZeasleosA0OiOVg03Q==", "_id" : ObjectId("520aeb7a8fa98f8c1d000001"), "__v" : 0 }
{ "name" : "demo", "password" : "vL4zZeasleosA0OiOVg03Q==", "_id" : ObjectId("520b372544f01e981d000002"), "__v" : 0 }
> db.users.find({name:'admin'});
{ "name" : "admin", "password" : "vL4zZeasleosA0OiOVg03Q==", "_id" : ObjectId("520adf6a00ae8a2008000001"), "__v" : 0 }



然後是寫了個網站登錄、網站註冊、網站註銷的demo。
從demo裏面也看得出純手工操作mongodb是比較麻煩的。
於是便把mongoose找來。npm install mongoose 或在package.json裏面的dependencies里加mongoose:"*"


然後就是對照mongoose官網翻閱了一翻,先連接mongodb,然後寫Schame,然後Model。最後exports出去。這樣mongoose的工作就完了。沒什麼比較麻煩的東西,主要是Schame的定義以及Query查詢。

  這裏給一個Model的例子

  

/**
 * Created with JetBrains WebStorm.
 * User: linpeng
 * Date: 13-8-9
 * Time: PM 2:11
 */
var mongoose = require('mongoose');
var config = require('./../config');

var nameProperty = { type: String, index: { unique: true }};
var userSchema = mongoose.Schema({
    name:nameProperty, password:String
});

var User = mongoose.model('user',userSchema);

module.exports = User;



又發現網站需要flash操作。於是npm install flashify


npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/flashify
npm http 200 https://registry.npmjs.org/flashify
npm http GET https://registry.npmjs.org/flashify/-/flashify-0.1.2.tgz
npm http 200 https://registry.npmjs.org/flashify/-/flashify-0.1.2.tgz
[email protected] node_modules\flashify



整體來說,nodejs、express、mongoose操作都不復雜,主要難點在於集成在一起,js調試比較麻煩,ide我推薦webstorm。然後就是nodejs本身的異步處理,有些異步方法是在不根據代碼順序執行的,有時候需要注意一下,要把依賴異步處理結果的執行步驟,放在異步處理方法體內。這些問題出現在連接數據庫,讀磁盤等比較費時費力的操作上面。

        經過nodejs這一遭,對函數式有了初步的瞭解。與 java不同,函數式講究的是返回結果,一個函數就是一個操作集,操作結束,給個結果。是動詞,更少的名詞,與指令式有本質區別。


   學習資料:

mongoose http://mongoosejs.com/index.html

jade語法(英文)http://naltatis.github.io/jade-syntax-docs/

中文jade https://github.com/livereload/livereload-plugins/blob/master/Jade.lrplugin/node_modules/jade/Readme_zh-cn.md


              

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章