Github 參考 昨天先把專案目錄初始化,裝完該裝的套件後 在 routes 中加入一個 index.js 檔案 'use strict'; var express = require('express'); var router = express.Router(); router.get('/', function(req, res) { res.status(200).json({ greetings: "hello world!"}); }); module.exports = router; 建立一個 app.js 在專案根目錄中: app.js var express = require('express'); var app = express(); var index = require('./routes/index'); // Set routers app.use('/', index); module.exports = app; 然後在 bin 資料夾建立一個 server.js: bin/server.js: 'use strict'; var debug = require('debug')('TODOAPI'); var app = require('.

Continue reading

會使用到的套件: 主體: express - web framework body-parser - parse request body 測試相關: mocha - test framework mocha-mongoose - 用來和 mocha 和 mongoose 中間做介接的套件 superagent - 拿它來丟 http request validator - 用來驗證某個物件是否為某個型別 expect.js - 類似 BDD 的語法,用來寫測試的 debug - debug package DB 相關: mongoose - mongodb orm mongodb - mongodb native driver 開發相關: nodemon - detect file change and restart server 生產環境相關: log4js - 記錄 request log 這是我的 package.json, 可以複製到專案目錄,然後下 npm install

Continue reading

首先我們就在虛擬機的資料夾底下,建立一個專案資料夾吧! 今天會來簡單切割一下我們的專案目錄 建立新專案目錄: $ cd /vagrant/ $ mkdir TODOAPI $ cd TODOAPI $ npm init // 輸入專案資訊 $ 建立專案結構 / | - bin/ | - routes/ | - model/ | - node_modules/ | - logs/ | - config/ | - test/ | - app.js | - package.json 分別講解一下各個 folder 的工作: bin 筆者會在 bin 資料夾底下放一個 server.js 這個 server.js 就是會專門用來啟動一個 app.js instance routes 基本上如果了解 MVC 架構的話,routes 就是 controller 的意思,其實如果讀者本身夠了解的話,這個資料夾也可以改成 controller 也 ok,應該說,其實資料夾怎麼放都是看個人隨意即可,只是若是要模組化的話,筆者會這樣來處理一個專案結構。

Continue reading

今天要使用 Node.js 的 mongodb driver 來操作 DB 一樣先開啟虛擬機並且登入吧 會使用到的套件是: mongodb -> mongodb native driver 或許有人有看過 mongoose -> 官網 算是 MongoDB 的 ORM,我們在這邊如果有時間的話再簡單了解一下, 現在先使用 native driver 來操作 我們在這邊就不裝全域套件了,裝在 API 專案裡面 並且使用 -save 來將相依性寫入 package.json 裏 $ cd /vagrant/API/ $ npm install mongodb -save 從虛擬機回到電腦的資料夾中,在虛擬機資料夾的 API 資料夾中, 今天我們先不和 express 搭配,因此就是純操作 node.js + mongodb module 新增一個 mongoTest.js 我們先來連線到 MongoDB,連線的方式有很多,下面是其中一種方式, // 先宣告用的到的東西 var MongoClient = require('mongodb').MongoClient , Server = require('mongodb').Server , options = { auto_reconnection: true, poolSize: 10 }; // Server 設定 var mongoClient = new MongoClient(new Server('localhost', 27017, options)); // 開啟連線 mongoClient.

Continue reading

Express - Hello World! 今天就要來安裝主角之一的 Express 了! 一樣開啟虛擬機器我應該是可以不用多說了 XD 先確認自己的 node 是不是 NVM 的 node 喲 建立專案 $ cd /vagrant/ $ mkdir API $ cd API/ $ npm init 自己輸入專案的相關內容吧,不知道的就直接按 enter 空白丟給他~ 就會產生 package.json 了! 安裝 Express // 在專案目錄底下 (/vagrant/API/) $ npm install express -save 下 -save 的原因是要將 express 加入 package.json 中 離開虛擬機,其實我們也可以在虛擬機資料夾看到剛剛建立的專案了, 現在開始就可以使用自己喜歡的編輯器來開啟這個專案資料夾,我個人偏好使用 Sublime Text 要執行專案時再回終端機即可 Express Hello World! 在 API 資料夾中建立一個 app.js 內容是: javascript var express = require('express'); var app = express(); app.

Continue reading

Node.js - Hello World! 一樣先開虛擬機哦~ 並且開啟終端機連進 server 裡面~ 以下是 Node.js 官方網站的 web server 程式範例,我稍作小修改: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '0.0.0.0'); console.log('Server running at http://0.0.0.0:1337/'); 我們先在虛擬機的資料夾建立一個子資料夾 ‘HelloWorld’ 在 HelloWorld 資料夾裡面建立一個 app.js 內容如上,存檔。 使用終端機進入 server : $ vagrant ssh $ cd /vagrant/HelloWorld $ node app.js 到瀏覽器輸入 http://192.168.33.10:1337/ (或者是你的 Vagrantfile 裡面設定的 private IP) 即可看到 Hello World 了! 要關掉 Server 也非常簡單,回到下指令的地方,按下 ctrl + c 即可中斷程式

Continue reading

Event-driven I/O model 首先,聽到 Event-driven 是從 Node.js 得知, Node.js® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. 其實剛聽到這詞會有點陌生,我們可以先從 Victor 所寫的文章開始讀起: 淺談coroutine與gevent 裡面提到了幾種網路模型: > 1. 阻塞式單一行程 2. 阻塞式多行程 3. 阻塞式多行程多執行序 4. 非阻塞式事件驅動 5. 非阻塞式 coroutine 以下是各語言 event-driven 的 model

Continue reading

Author's picture

kerkerj

Cat lover <3

Backend Engineer

Taiwan