@bedrock/server

中文文档
5.2.1发布于 2026/09/18Apache-2.0ES Module近 30 天下载 4106

Bedrock core server module

原始使用文档
文档目录7 节
  1. Requirements
  2. Quick Examples
  3. Configuration
  4. Setup
  5. Bedrock Events
  6. How It Works
  7. License

bedrock-server

A bedrock module that provides a basic HTTP and HTTPS server. Other modules, such as bedrock-express, typically provide a routing framework and other features for writing Web applications, but depend on this module for core low-level functionality like listening for incoming connections, redirecting HTTP traffic to the HTTPS port, and configuring SSL/TLS.

Requirements

This software requires and supports maintained recent versions of Node.js. Updates may remove support for older unmaintained platform versions. Please use dependency version lock files and testing to ensure compatibility with this software.

Quick Examples

npm install @bedrock/server

An example of attaching a custom request handler to the server once Bedrock is ready.

import * as bedrock from '@bedrock/core';
import * as server from '@bedrock/server';

// once bedrock is ready, attach request handler
bedrock.events.on('bedrock.ready', function() {
  // attach to TLS server
  server.servers.https.on('request', function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  });
});

bedrock.start();

By default, @bedrock/server will redirect any HTTP requests to HTTPS. To replace this default behavior, do the following:

import * as server from '@bedrock/server';

// once bedrock is ready, attach request handler
bedrock.events.on('bedrock.ready', function() {
  // attach to HTTP server
  server.servers.http.on('request', function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  });
});

bedrock.start();

Configuration

For documentation on server configuration, see config.js.

Setup

  1. [optional] Tweak configuration
  2. Map the bedrock.localhost hostname (or whatever you've configured) to your machine:
    1. Edit the /etc/hosts file as the administrator/root.
    2. Add an entry mapping the IP address to bedrock.localhost. For example: 127.0.0.1 localhost bedrock.localhost. (If accessing the server externally, you may need to use the IP address of your primary network device).

To access the server once bedrock is running:

  1. Go to: https://bedrock.localhost:18443/
  2. The certificate warning is normal for development mode. Accept it and continue.

Bedrock Events

List of emitted Bedrock Events:

  • bedrock-server.readinessCheck
    • Emitted before listening starts on any ports.
  • bedrock-server.http.listen
    • Arguments:
      • {address, port}: Object with address and port to listen on.
    • Emitted before listening on a HTTP port.
  • bedrock-server.http.listening
    • Arguments:
      • {address, port}: Object with address and port now listening on.
    • Emitted after listening on a HTTP port.
  • bedrock-server.https.listen
    • Arguments:
      • {address, port}: Object with address and port to listen on.
    • Emitted before listening on a HTTPS port.
  • bedrock-server.https.listening
    • Arguments:
      • {address, port}: Object with address and port now listening on.
    • Emitted after listening on a HTTPS port.
  • bedrock-server.ready
    • Emitted after listening is complete.

How It Works

TODO

License

Apache License, Version 2.0 Copyright 2011-2024 Digital Bazaar, Inc.

Other Bedrock libraries are available under a non-commercial license for uses such as self-study, research, personal projects, or for evaluation purposes. See the Bedrock Non-Commercial License v1.0 for details.

Commercial licensing and support are available by contacting Digital Bazaar [email protected].

版本与发布记录

@bedrock/server 共发布 7 个版本,首次发布于 2022/04/02;过去 12 个月发布了 2 个版本。

版本发布日期距今
5.2.1latest2026/09/181 个月前
5.2.02026/08/222 个月前
5.1.02024/02/292 年前
5.0.02022/04/294 年前
4.1.02022/04/214 年前
4.0.12022/04/024 年前

关于 @bedrock/server 的常见问题

@bedrock/server 是什么?

Bedrock core server module

如何安装 @bedrock/server?

在项目目录运行 npm install @bedrock/server,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bedrock/core(^6.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bedrock/server 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bedrock__server 是否存在。

@bedrock/server 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bedrock/server 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bedrock/server 可以商用吗?

@bedrock/server 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bedrock/server 的下载量有多少?

根据 npm 官方下载统计,@bedrock/server 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,106 次,日均约 137 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bedrock/server 还在维护吗?

最新版本 5.2.1 发布于 2026/09/18(1 个月前);至今共发布 7 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。