天天看點

Express 的使用Node.js 的 ExpressHello World應用 Application請求 Request響應 Response我會怎麼用 Express日志ini 格式配置WebSocket其它

以下内容,基于 Express 4.x 版本

Node.js 的 Express

Express 估計是那種你第一次接觸,就會喜歡上用它的架構。因為它真的非常簡單,直接。

在目前版本上,一共才這麼幾個檔案:

lib/
├── application.js
├── express.js
├── middleware
│   ├── init.js
│   └── query.js
├── request.js
├── response.js
├── router
│   ├── index.js
│   ├── layer.js
│   └── route.js
├── utils.js
└── view.js           

這種程度,說它是一個“架構”可能都有些過了,幾乎都是工具性質的實作,隻限于 Web 層。

當然,直接了當地實作了 Web 層的基本功能,是得益于 Node.js 本身的 API 中,就提供了 net 和 http 這兩層, Express 對 http 的方法包裝一下即可。

不過,本身功能簡單的東西,在

package.json

中卻有好長一串 dependencies 清單。

Hello World

在跑 Express 前,你可能需要初始化一個 npm 項目,然後再使用 npm 安裝 Express:

mkdir p
cd p
npm init
npm install express --save           

建立一個

app.js

const express = require('express');
const app = express();
app.all('/', (req, res) => res.send('hello') );
app.listen(8888);           

調試資訊是通過環境變量 DEBUG 控制的:

const process = require('process');
process.env['DEBUG'] = 'express:*';           

這樣就可以在終端看到帶顔色的輸出了,嗯,是的,帶顔色控制字元,vim 中直接跑就 SB 了。

應用 Application

Application 是一個上層統籌的概念,整合“請求-響應”流程。

express()

的調用會傳回一個 application ,一個項目中,有多個 app 是沒問題的:

const express = require('express');

const app = express();
app.all('/', (req, res) => res.send('hello'));
app.listen(8888);

const app2 = express();
app2.all('/', (req, res) => res.send('hello2'));
app2.listen(8889);           

多個 app 的另一個用法,是直接把某個 path 映射到整個 app :

const express = require('express');

const app = express();

app.all('/', (req, res) => {
    res.send('ok');
});

const app2 = express();
app2.get('/xx', (req, res, next) => res.send('in app2') )
app.use('/2', app2)

app.listen(8888);           

這樣,當通路

/2/xx

時,就會看到

in app2

的響應。

前面說了 app 實際上是一個上層排程的角色,在看後面的内容之前,先說一下 Express 的特點,整體上來說,它的結構基本上是“回調函數串行”,無論是 app ,或者 route, handle, middleware 這些不同的概念,它們的形式,基本是一緻的,就是

(res, req, next) => {}

,串行的流程依賴

next()

的顯式調用。

我們把 app 的功能,分成五個部分來說。

路由 - Handler 映射

app.all('/', (req, res, next) => {});
app.get('/', (req, res, next) => {});
app.post('/', (req, res, next) => {});
app.put('/', (req, res, next) => {});
app.delete('/', (req, res, next) => {});           

上面的代碼就是基本的幾個方法,路由的比對是串行的,可以通過

next()

控制:

const express = require('express');

const app = express();

app.all('/', (req, res, next) => {
    res.send('1 ');
    console.log('here');
    next();
});

app.get('/', (req, res, next) => {
    res.send('2 ');
    console.log('get');
    next();
});

app.listen(8888);           

對于上面的代碼,因為重複調用

send()

會報錯。

同樣的功能,也可以使用

app.route()

來實作:

const express = require('express');

const app = express();

app.route('/').all( (req, res, next) => {
    console.log('all');
    next();
}).get( (req, res, next) => {
    res.send('get');
    next();
}).all( (req, res, next) => {
    console.log('tail');
    next();
});

app.listen(8888);           

app.route()

也是一種抽象通用邏輯的形式。

還有一個方法是

app.params

,它把“命名參數”的處理單獨拆出來了(我個人不了解這玩意兒有什麼用):

const express = require('express');

const app = express();

app.route('/:id').all( (req, res, next) => {
    console.log('all');
    next();
}).get( (req, res, next) => {
    res.send('get');
    next()
}).all( (req, res, next) => {
    console.log('tail');
});

app.route('/').all( (req, res) => {res.send('ok')});

app.param('id', (req, res, next, value) => {
    console.log('param', value);
    next();
});

app.listen(8888);           

app.params

中的對應函數會先行執行,并且,記得顯式調用

next()

Middleware

其實前面講了一些方法,要實作 Middleware 功能,隻需要

app.all(/.*/, () => {})

就可以了, Express 還專門提供了

app.use()

做通用邏輯的定義:

const express = require('express');

const app = express();

app.all(/.*/, (req, res, next) => {
    console.log('reg');
    next();
});

app.all('/', (req, res, next) => {
    console.log('pre');
    next();
});

app.use((req, res, next) => {
    console.log('use');
    next();
});

app.all('/', (req, res, next) => {
    console.log('all');
    res.send('/ here');
    next();
});

app.use((req, res, next) => {
    console.log('use2');
    next();
});

app.listen(8888);           

注意

next()

的顯式調用,同時,注意定義的順序,

use()

all()

順序上是平等的。

Middleware 本身也是

(req, res, next) => {}

這種形式,自然也可以和 app 有對等的機制——接受路由過濾, Express 提供了 Router ,可以單獨定義一組邏輯,然後這組邏輯可以跟 Middleware 一樣使用。

const express = require('express');
const app = express();
const router = express.Router();

app.all('/', (req, res) => {
    res.send({a: '123'});
});

router.all('/a', (req, res) => {
    res.send('hello');
});

app.use('/route', router);

app.listen(8888);           

功能開關,變量容器

app.set()

app.get()

可以用來儲存 app 級别的變量(對,

app.get()

還和 GET 方法的實作名字上還沖突了):

const express = require('express');

const app = express();

app.all('/', (req, res) => {
    app.set('title', '标題123');
    res.send('ok');
});

app.all('/t', (req, res) => {
    res.send(app.get('title'));
});


app.listen(8888);           

上面的代碼,啟動之後直接通路

/t

是沒有内容的,先通路

/

再通路

/t

才可以看到内容。

對于變量名, Express 預置了一些,這些變量的值,可以叫 settings ,它們同時也影響整個應用的行為:

  • case sensitive routing

  • env

  • etag

  • jsonp callback name

  • json escape

  • json replacer

  • json spaces

  • query parser

  • strict routing

  • subdomain offset

  • trust proxy

  • views

  • view cache

  • view engine

  • x-powered-by

具體的作用,可以參考

https://expressjs.com/en/4x/api.html#app.set

(上面這些值中,幹嘛不放一個最基本的 debug 呢……)

除了基本的

set() / get()

,還有一組

enable() / disable() / enabled() / disabled()

的包裝方法,其實就是

set(name, false)

這種。

set(name)

這種隻傳一個參數,也可以擷取到值,等于

get(name)

模闆引擎

Express 沒有自帶模闆,是以模闆引擎這塊就被設計成一個基礎的配置機制了。

const process = require('process');
const express = require('express');
const app = express();

app.set('views', process.cwd() + '/template');

app.engine('t2t', (path, options, callback) => {
    console.log(path, options);
    callback(false, '123');
});

app.all('/', (req, res) => {
    res.render('demo.t2t', {title: "标題"}, (err, html) => {
        res.send(html)
    });
});

app.listen(8888);           

app.set('views', ...)

是配置模闆在檔案系統上的路徑,

app.engine()

是擴充名為辨別,注冊對應的處理函數,然後,

res.render()

就可以渲染指定的模闆了。

res.render('demo')

這樣不寫擴充名也可以,通過

app.set('view engine', 't2t')

可以配置預設的擴充名。

這裡,注意一下

callback()

的形式,是

callback(err, html)

端口監聽

app 功能的最後一部分,

app.listen()

,它完成的形式是:

app.listen([port[, host[, backlog]]][, callback])           

注意,

host

是第二個參數。

backlog

是一個數字,配置可等待的最大連接配接數。這個值同時受作業系統的配置影響。預設是 512 。

請求 Request

這一塊倒沒有太多可以說的,一個請求你想知道的資訊,都被包裝到

req

的屬性中的。除了,頭。頭的資訊,需要使用

req.get(name)

來擷取。

GET 參數

使用

req.query

可以擷取 GET 參數:

const express = require('express');
const app = express();

app.all('/', (req, res) => {
    console.log(req.query);
    res.send('ok');
});

app.listen(8888);           

請求:

# -*- coding: utf-8 -*-
import requests
requests.get('http://localhost:8888', params={"a": '中文'.encode('utf8')})           

POST 參數

POST 參數的擷取,使用

req.body

,但是,在此之前,需要專門挂一個 Middleware ,

req.body

才有值:

const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));
app.all('/', (req, res) => {
    console.log(req.body);
    res.send('ok');
});

app.listen(8888);           
# -*- coding: utf-8 -*-

import requests

requests.post('http://localhost:8888', data={"a": '中文'})           

如果你是整塊扔的 json 的話:

# -*- coding: utf-8 -*-

import requests
import json

requests.post('http://localhost:8888', data=json.dumps({"a": '中文'}),
              headers={'Content-Type': 'application/json'})           

Express 中也有對應的

express.json()

來處理:

const express = require('express');
const app = express();

app.use(express.json());
app.all('/', (req, res) => {
    console.log(req.body);
    res.send('ok');
});

app.listen(8888);           

Express 中處理

body

部分的邏輯,是單獨放在

body-parser

這個 npm 子產品中的。 Express 也沒有提供方法,友善地擷取原始 raw 的内容。另外,對于 POST 送出的編碼資料, Express 隻支援 UTF-8 編碼。

如果你要處理檔案上傳,嗯, Express 沒有現成的 Middleware ,額外的實作在

https://github.com/expressjs/multer

。( Node.js 天然沒有“位元組”類型,是以在位元組級别的處理上,就會感覺很不順啊)

Cookie

Cookie 的擷取,也跟 POST 參數一樣,需要外挂一個

cookie-parser

子產品才行:

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser())
app.all('/', (req, res) => {
    console.log(req.cookies);
    res.send('ok');
});

app.listen(8888);           
# -*- coding: utf-8 -*-

import requests
import json

requests.post('http://localhost:8888', data={'a': '中文'},
              headers={'Cookie': 'a=1'})           

如果 Cookie 在響應時,是配置 res 做了簽名的,則在 req 中可以通過

req.signedCookies

處理簽名,并擷取結果。

來源 IP

Express 對

X-Forwarded-For

頭,做了特殊處理,你可以通過

req.ips

擷取這個頭的解析後的值,這個功能需要配置

trust proxy

這個 settings 來使用:

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser())
app.set('trust proxy', true);
app.all('/', (req, res) => {
    console.log(req.ips);
    console.log(req.ip);
    res.send('ok');
});

app.listen(8888);           
# -*- coding: utf-8 -*-

import requests
import json

#requests.get('http://localhost:8888', params={"a": '中文'.encode('utf8')})
requests.post('http://localhost:8888', data={'a': '中文'},
              headers={'X-Forwarded-For': 'a, b, c'})           

如果

trust proxy

不是

true

,則

req.ip

會是一個 ipv4 或者 ipv6 的值。

響應 Response

Express 的響應,針對不同類型,本身就提供了幾種包裝了。

普通響應

res.send

處理确定性的内容響應:

res.send({ some: 'json' });
res.send('<p>some html</p>');
res.status(404); res.end();
res.status(500); res.end();           

res.send()

會自動

res.end()

,但是,如果隻使用

res.status()

的話,記得加上

res.end()

模闆渲染

模闆需要預先配置,在 Request 那節已經介紹過了。

const process = require('process');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser())

app.set('trust proxy', false);
app.set('views', process.cwd() + '/template');
app.set('view engine', 'html');
app.engine('html', (path, options, callback) => {
    callback(false, '<h1>Hello</h1>');
});

app.all('/', (req, res) => {
    res.render('index', {}, (err, html) => {
        res.send(html);
    });
});

app.listen(8888);           

這裡有一個坑點,就是必須在對應的目錄下,有對應的檔案存在,比如上面例子的

template/index.html

,那麼

app.engine()

中的回調函數才會執行。都自定義回調函數了,這個限制沒有任何意義,

path, options

傳入就好了,至于是不是要通過檔案系統讀取内容,怎麼讀取,又有什麼關系呢。

res.cookie

來處理 Cookie 頭:

const process = require('process');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser("key"))

app.set('trust proxy', false);
app.set('views', process.cwd() + '/template');
app.set('view engine', 'html');
app.engine('html', (path, options, callback) => {
    callback(false, '<h1>Hello</h1>');
});
app.all('/', (req, res) => {
    res.render('index', {}, (err, html) => {
        console.log('cookie', req.signedCookies.a);
        res.cookie('a', '123', {signed: true});
        res.cookie('b', '123', {signed: true});
        res.clearCookie('b');
        res.send(html);
    });
});

app.listen(8888);           
# -*- coding: utf-8 -*-

import requests
import json

res = requests.post('http://localhost:8888', data={'a': '中文'},
                    headers={'X-Forwarded-For': 'a, b, c',
                             'Cookie': 'a=s%3A123.p%2Fdzmx3FtOkisSJsn8vcg0mN7jdTgsruCP1SoT63z%2BI'})
print(res, res.text, res.headers)           

注意三點:

  • app.use(cookieParser("key"))

    這裡必須要有一個字元串做 key ,才可以正确使用簽名的 cookie 。
  • clearCookie()

    仍然是用“設定過期”的方式來達到删除目的,

    cookie()

    clearCookie()

    并不會整合,會寫兩組

    b=xx

    進頭。
  • res.send()

    會在連接配接上完成一個響應,是以,與頭相關的操作,都必須放在

    res.send()

    前面。

頭和其它

res.set()

可以設定指定的響應頭,

res.rediect(301, 'http://www.zouyesheng.com')

處理重定向,

res.status(404); res.end()

處理非 20 響應。

const process = require('process');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser("key"))

app.set('trust proxy', false);
app.set('views', process.cwd() + '/template');
app.set('view engine', 'html');
app.engine('html', (path, options, callback) => {
    callback(false, '<h1>Hello</h1>');
});

app.all('/', (req, res) => {
    res.render('index', {}, (err, html) => {
        res.set('X-ME', 'zys');
        //res.redirect('back');
        //res.redirect('http://www.zouyesheng.com');
        res.status(404);
        res.end();
    });
});

app.listen(8888);           

res.redirect('back')

會自動擷取

referer

頭作為

Location

的值,使用這個時,注意

referer

為空的情況,會造成循環重複重定向的後果。

Chunk 響應

Chunk 方式的響應,指連接配接建立之後,服務端的響應内容是不定長的,會加個頭:

Transfer-Encoding: chunked

,這種狀态下,服務端可以不定時往連接配接中寫入内容(不排除服務端的實作會有緩沖區機制,不過我看 Express 沒有)。

const process = require('process');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser("key"))

app.set('trust proxy', false);
app.set('views', process.cwd() + '/template');
app.set('view engine', 'html');
app.engine('html', (path, options, callback) => {
    callback(false, '<h1>Hello</h1>');
});

app.all('/', (req, res) => {
    const f = () => {
        const t = new Date().getTime() + '\n';
        res.write(t);
        console.log(t);
        setTimeout(f, 1000);
    }

    setTimeout(f, 1000);
});

app.listen(8888);           

上面的代碼,通路之後,每過一秒,都會收到新的内容。

大概是

res

本身是 Node.js 中的 stream 類似對象,是以,它有一個

write()

方法。

要測試這個效果,比較友善的是直接 telet:

zys@zys-alibaba:/home/zys/temp >>> telnet localhost 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost

HTTP/1.1 200 OK
X-Powered-By: Express
Date: Thu, 20 Jun 2019 08:11:40 GMT
Connection: keep-alive
Transfer-Encoding: chunked

e
1561018300451

e
1561018301454

e
1561018302456

e
1561018303457

e
1561018304458

e
1561018305460

e
1561018306460           

每行前面的一個位元組的

e

,為 16 進制的 14 這個數字,也就是後面緊跟着的内容的長度,是 Chunk 格式的要求。具體可以參考 HTTP 的 RFC ,

https://tools.ietf.org/html/rfc2616#page-2

Tornado 中的類似實作是:

# -*- coding: utf-8 -*-

import tornado.ioloop
import tornado.web
import tornado.gen
import time

class MainHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        while True:
            yield tornado.gen.sleep(1)
            s = time.time()
            self.write(str(s))
            print(s)
            yield self.flush()


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()           

Express 中的實作,有個大坑,就是:

app.all('/', (req, res) => {
    const f = () => {
        const t = new Date().getTime() + '\n';
        res.write(t);
        console.log(t);
        setTimeout(f, 1000);
    }

    setTimeout(f, 1000);
});           

這段邏輯,在連接配接已經斷了的情況下,并不會停止,還是會永遠執行下去。是以,你得自己處理好:

const process = require('process');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser("key"))

app.set('trust proxy', false);
app.set('views', process.cwd() + '/template');
app.set('view engine', 'html');
app.engine('html', (path, options, callback) => {
    callback(false, '<h1>Hello</h1>');
});

app.all('/', (req, res) => {
    let close = false;
    const f = () => {
        const t = new Date().getTime() + '\n';
        res.write(t);
        console.log(t);
        if(!close){
            setTimeout(f, 1000);
        }
    }

    req.on('close', () => {
        close = true;
    });

    setTimeout(f, 1000);
});

app.listen(8888);           

req

挂了一些事件的,可以通過

close

事件來得到目前連接配接是否已經關閉了。

req

上直接挂連接配接事件,從

net

http

Express

這個層次結構上來說,也很,尴尬了。 Web 層不應該關心到網絡連接配接這麼底層的東西的。

我還是習慣這樣:

app.all('/', (req, res) => {
    res.write('<h1>123</h1>');
    res.end();
});           

不過

res.write()

是不能直接處理 json 對象的,還是老老實實

res.send()

吧。

我會怎麼用 Express

先說一下,我自己,目前在 Express 運用方面,并沒有太多的時間和複雜場景的積累。

即使這樣,作為技術上相對傳統的人,我會以我以往的 web 開發的套路,來使用 Express 。

我不喜歡日常用

app.all(path, callback)

這種形式去組織代碼。

首先,這會使

path

定義散落在各處,友善了開發,麻煩了維護。

其次,把

path

和具體實作邏輯

callback

綁在一起,我覺得也是反思維的。至少,對于我個人來說,開發的過程,先是想如何實作一個 handler ,最後,再是考慮要把這個 handle 與哪些

path

綁定。

再次,單純的

callback

缺乏層次感,用

app.use(path, callback)

這種來處理共用邏輯的方式,我覺得完全是扯談。共用邏輯是代碼之間本身實作上的關系,硬生生跟網絡應用層 HTTP 協定的

path

概念抽上關系,何必呢。當然,對于

callback

的組織,用純函數來串是可以的,不過我在這方面并沒有太多經驗,是以,我還是選擇用類繼承的方式來作階層化的實作。

我自己要用 Express ,大概會這樣元件項目代碼(不包括關系資料庫的 Model 抽象如何組織這部分):

./
├── config.conf
├── config.js
├── handler
│   ├── base.js
│   └── index.js
├── middleware.js
├── server.js
└── url.js           
  • config.conf

    是 ini 格式的項目配置。
  • config.js

    處理配置,包括日志,資料庫連接配接等。
  • middleware.js

    是針對整體流程的擴充機制,比如,給每個請求加一個 UUID ,每個請求都記錄一條日志,日志内容有請求的細節及本次請求的處理時間。
  • server.js

    是主要的服務啟動邏輯,整合各種資源,指令行參數 port 控制監聽哪個端口。不需要考慮多程序問題,(正式部署時 nginx 反向代理到多個應用執行個體,多個執行個體及其它資源統一用 supervisor 管理)。
  • url.js

    定義路徑與 handler 的映射關系。
  • handler

    ,具體邏輯實作的地方,所有

    handler

    都從

    BaseHandler

    繼承。

BaseHandler

的實作:

class BaseHandler {
    constructor(req, res, next){
        this.req = req;
        this.res = res;
        this._next = next;
        this._finised = false;
    }

    run(){
        this.prepare();
        if(!this._finised){
            if(this.req.method === 'GET'){
                this.get();
                return;
            }
            if(this.req.method === 'POST'){
                this.post();
                return;
            }
            throw Error(this.req.method + ' this method had not been implemented');
        }
    }

    prepare(){}
    get(){
        throw Error('this method had not been implemented');
    }
    post(){
        throw Error('this method had not been implemented');
    }

    render(template, values){
        this.res.render(template, values, (err, html) => {
            this.finish(html);
        });
    }

    write(content){
        if(Object.prototype.toString.call(content) === '[object Object]'){
            this.res.write(JSON.stringify(content));
        } else {
            this.res.write(content);
        }
    }

    finish(content){
        if(this._finised){
            throw Error('this handle was finished');
        }
        this.res.send(content);
        this._finised = true;
        if(this._next){ this._next() }
    }

}

module.exports = {BaseHandler};

if(module === require.main){
    const express = require('express');
    const app = express();
    app.all('/', (req, res, next) => new BaseHandler(req, res, next).run() );
    app.listen(8888);
}           

要用的話,比如

index.js

const BaseHandler = require('./base').BaseHandler;

class IndexHandler extends BaseHandler {
    get(){
        this.finish({a: 'hello'});
    }
}

module.exports = {IndexHandler};           

url.js

中的樣子:

const IndexHandler = require('./handler/index').IndexHandler;

const Handlers = [];

Handlers.push(['/', IndexHandler]);

module.exports = {Handlers};           

日志

後面這幾部分,都不屬于 Express 本身的内容了,隻是我個人,随便想到的一些東西。

找一個日志子產品的實作,功能上,就看這麼幾點:

  • 标準的級别: DEBUG,INFO,WARN, ERROR 這些。
  • 層級的多個 logger 。
  • 可注冊式的多種 Handler 實作,比如檔案系統,作業系統的 rsyslog ,标準輸出,等。
  • 格式定義,一般都帶上時間和代碼位置。

Node.js 中,大概就是 log4js 了,

https://github.com/log4js-node/log4js-node
const log4js = require('log4js');

const layout = {
    type: 'pattern',
    pattern: '- * %p * %x{time} * %c * %f * %l * %m',
    tokens: {
        time: logEvent => {
            return new Date().toISOString().replace('T', ' ').split('.')[0];
        }
    }
};
log4js.configure({
  appenders: {
        file: { type: 'dateFile', layout: layout, filename: 'app.log', keepFileExt: true },
        stream: { type: 'stdout', layout: layout }
  },
  categories: {
      default: { appenders: [ 'stream' ], level: 'info', enableCallStack: false },
      app: { appenders: [ 'stream', 'file' ], level: 'info', enableCallStack: true }
  }
});

const logger = log4js.getLogger('app');
logger.error('xxx');

const l2 = log4js.getLogger('app.good');
l2.error('ii');           

總的來說,還是很好用的,但是官網的文檔不太好讀,有些細節的東西沒講,好在源碼還是比較簡單。

說幾點:

  • getLogger(name)

    需要給一個名字,否則

    default

    的規則都比對不到。
  • getLogger('parent.child')

    中的名字,規則比對上,可以通過

    .

    作父子繼承的。
  • enableCallStack: true

    加上,才能拿到檔案名和行号。

ini 格式配置

json 作配置檔案,功能上沒問題,但是對人為修改是不友好的。是以,個人還是喜歡用 ini 格式作項目的環境配置檔案。

Node.js 中,可以使用 ini 子產品作解析:

const s = `
[database]
host = 127.0.0.1
port = 5432
user = dbuser
password = dbpassword
database = use_this_database

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
`

const fs = require('fs');
const ini = require('ini');

const config = ini.parse(s);
console.log(config);           

它擴充了

array[]

這種格式,但沒有對類型作處理(除了

true

false

),比如,擷取

port

,結果是

"5432"

。簡單夠用了。

WebSocket

Node.js 中的 WebSocket 實作,可以使用 ws 子產品,

https://github.com/websockets/ws

要把 ws 的 WebSocket Server 和 Express 的 app 整合,需要在 Express 的 Server 層面動手,實際上這裡說的 Server 就是 Node.js 的 http 子產品中的

http.createServer()

const express = require('express');
const ws = require('ws');

const app = express();

app.all('/', (req, res) => {
    console.log('/');
    res.send('hello');
});

const server = app.listen(8888);

const wss = new ws.Server({server, path: '/ws'});
wss.on('connection', conn => {
    conn.on('message', msg => {
        console.log(msg);
        conn.send(new Date().toISOString());
    });
});           

對應的一個用戶端實作,來自:

https://github.com/ilkerkesen/tornado-websocket-client-example/blob/master/client.py
# -*- coding: utf-8 -*-

import time
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado import gen
from tornado.websocket import websocket_connect

class Client(object):
    def __init__(self, url, timeout):
        self.url = url
        self.timeout = timeout
        self.ioloop = IOLoop.instance()
        self.ws = None
        self.connect()
        PeriodicCallback(self.keep_alive, 2000).start()
        self.ioloop.start()

    @gen.coroutine
    def connect(self):
        print("trying to connect")
        try:
            self.ws = yield websocket_connect(self.url)
        except Exception:
            print("connection error")
        else:
            print("connected")
            self.run()

    @gen.coroutine
    def run(self):
        while True:
            msg = yield self.ws.read_message()
            print('read', msg)
            if msg is None:
                print("connection closed")
                self.ws = None
                break

    def keep_alive(self):
        if self.ws is None:
            self.connect()
        else:
            self.ws.write_message(str(time.time()))


if __name__ == "__main__":
    client = Client("ws://localhost:8888/ws", 5)           

其它

繼續閱讀