@9db/node-server 中文文档教程
node-server
9DB 服务器规范的参考实现,用 Node.js 编写。
Installation
npm install @9db/server --save
Usage
import {Server} from '@9db/server';
const server = new Server();
server.start();
您应该可以通过访问 http://localhost:9999 来访问服务器。
Options
以下选项可以传递给 new Server({ ... })
:
key | description | default value |
---|---|---|
port | The HTTP port that the 9DB server should bind to. | 9999 |
adapter | The database adapter to use for storing nodes. See the Adapters section below. | MemoryAdapter |
hostname | The hostname (protocol + FQDN) that this server will be hosted from. | http://localhost |
Adapters
服务器使用适配器来存储节点供以后使用。 通过显式 适配器通过以下方式连接到您的服务器:
const adapter = new MemoryAdapter();
const server = new Server({ adapter });
开箱即用有几种不同的适配器:
MemoryAdapter
此适配器仅将节点保留在本地进程内存中。 如果服务器崩溃 或重新启动,所有持久化节点都将丢失。 对本地发展有用 但在部署之前应更换为不同的适配器类型。
如果未指定显式适配器,则默认使用此适配器类型。
import {MemoryAdapter} from '@9db/server';
const adapter = new MemoryAdapter();
FilesystemAdapter
此适配器将节点保存到本地文件系统。 根据您的需要, 这种简单的持久性机制就足够了。 然而,情况并不好 适合服务于高流量站点,并且不能在负载平衡中运行 簇。
import {FilesystemAdapter} from '@9db/server';
const adapter = new FilesystemAdapter({
path: '/home/pachet/9db/cache'
});
RedisAdapter
此适配器将节点持久保存到 Redis 实例。 这是推荐的适配器 用于高流量站点。
import {RedisAdapter} from '@9db/server';
const adapter = new RedisAdapter({
hostname: 'localhost',
port: 6667
});
Creating your own adapter
您还可以指定自己的适配器,以便将您的 9DB 节点持久化到 所包含的适配器不支持的数据库。
适配器必须实现以下方法:
fetchNode(node_key)
storeNode(node_key, node)
setField(node_key, field_key, old_value, new_value)
node-server
A reference implementation of the 9DB server specification, written in Node.js.
Installation
npm install @9db/server --save
Usage
import {Server} from '@9db/server';
const server = new Server();
server.start();
You should be able to access the server by visiting http://localhost:9999.
Options
The following options can be passed to new Server({ ... })
:
key | description | default value |
---|---|---|
port | The HTTP port that the 9DB server should bind to. | 9999 |
adapter | The database adapter to use for storing nodes. See the Adapters section below. | MemoryAdapter |
hostname | The hostname (protocol + FQDN) that this server will be hosted from. | http://localhost |
Adapters
The server uses adapters in order to store nodes for later use. Pass an explicit adapter to your server via:
const adapter = new MemoryAdapter();
const server = new Server({ adapter });
There are several different adapters available out of the box:
MemoryAdapter
This adapter merely retains nodes in local process memory. If the server crashes or is restarted, all persisted nodes will be lost. Useful for local development but should be replaced with a different adapter type before you deploy.
If no explicit adapter is specified, this adapter type will be used by default.
import {MemoryAdapter} from '@9db/server';
const adapter = new MemoryAdapter();
FilesystemAdapter
This adapter persists nodes to the local filesystem. Depending on your needs, this simple persistence mechanism could be sufficient. However, it is not well suited to serving high-traffic sites, and cannot function in a load-balanced cluster.
import {FilesystemAdapter} from '@9db/server';
const adapter = new FilesystemAdapter({
path: '/home/pachet/9db/cache'
});
RedisAdapter
This adapter persists nodes to a Redis instance. This is the recommended adapter to use for high-traffic sites.
import {RedisAdapter} from '@9db/server';
const adapter = new RedisAdapter({
hostname: 'localhost',
port: 6667
});
Creating your own adapter
You can also specify your own adapter in order to persist your 9DB nodes to databases that are not supported by the included adapters.
An adapter must implement the following methods:
fetchNode(node_key)
storeNode(node_key, node)
setField(node_key, field_key, old_value, new_value)