Nodal

用 Node.js 轻松实现 API 服务。「API Services Made Easy With Node.js」

  • 所有者: keithwhor/nodal
  • 平台: Linux, Mac, Windows
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    2
      比較:

Github星跟蹤圖

Nodal

用 Node.js 轻松实现 API 服务

请访问 nodaljs.com 网站。

Nodal 是一个 Web 服务器和偏向于自己使用的框架,用于在 Node.js 中为 Web、移动或物联网应用构建以数据操作为中心的(创建、读取、更新、销毁)API 服务。

为什么是 Nodal?

Hello, Nodal — Building Node.js Servers for Everybody(你好,Nodal -- 为大家构建 Node.js 服务器)是我们的第一篇博文,帮助你熟悉框架创建背后的原因。 :)

Post Parse Prototyping 也是一篇精彩的读物,它解释了 Nodal 对于快速和简单的移动/物联网后端开发的好处。

概述

Nodal 建立在一个健壮的、可扩展的数据存储和检索 API 架构的思想基础上。它是一个有主见的、明确的、惯用的和高度可扩展的全方位服务框架,为您和您的团队考虑所有艰难的决定。这使你能够专注于在短时间内创建一个有效的产品,同时最大限度地减少技术债务。

Nodal 服务器并不意味着是单体的。它们是无状态和分布式的,旨在为你的需求服务,毫不费力地与你的数据层对接。虽然你可以用 Nodal 输出任何数据格式,但建议你将静态页面渲染等事情卸载到 CDN 等其他优化服务上。

点击这里查看第一个 Nodal 截屏。

无状态教条

值得注意的是,Nodal 是为无状态的 API 服务设计的。这意味着你不应该依赖特定进程中的内存来服务多个请求,而且 Nodal 将使用进程集群(即使在开发中)来积极阻止这种做法。如果你需要使用非结构化数据进行快速原型设计,请将 Nodal 连接到 PostgreSQL 数据库并使用 "JSON" 字段类型。如果你开始尝试在不同的请求中使用进程内内存,你会发现自己遇到了很多麻烦。

记住:一个输入,一个输出。处理模型状态的副作用应该通过你的数据库来管理。Nodal 不应该被用于流式(长轮询)请求,HTTP 请求和响应对象被故意混淆。

这也意味着你不能依赖套接字连接。如果你需要在你的应用程序中加入实时功能,应该有一个单独的服务器来负责这个工作。它可以与你的 Nodal API 服务器对接,甚至从它那里接收事件,但你的 API 服务器不应该与任何客户端有一个有状态的(长时间的)连接。

开始使用

开始使用 Nodal 是很容易的。

  1. nodejs.org 下载并安装最新的 Node 6.x 版本。
  2. 打开终端,并输入 npm install nodal -g。 (如果你得到一个错误,运行 sudo npm install nodal -g 或通过 以下指示 永久修复权限。
  3. 使用你的终端,访问你的项目文件夹。也许用 cd ~
  4. 运行 nodal new
  5. 按照屏幕上的指示,进入你的新项目目录并输入 nodal s

这就是了! 你的 Nodal Web 服务器已经启动并运行了。

连接你的数据库

一旦 Nodal 启动并运行,你很可能会想把你的项目连接到数据库。Nodal 包含了 Migrations、Query Composer 和完整的 PostgreSQL 集成。

首先,你需要安装 PostgreSQL。OS X 用户,我推荐使用 Postgres.app 作为你的开发环境。

一旦你安装了 Postgres,请确保运行:

$ createuser postgres -s

来创建一个没有密码的默认 postgres 超级用户。(Nodal 的配置是默认的)。

要开始使用你的数据库,请从以下几点开始:

$ nodal db:create

来创建数据库,然后:

$ nodal db:prepare

来为迁移做准备。

从这里开始,nodal db:migrate 运行所有未完成的迁移,nodal db:rollback 将回滚迁移,默认情况下是一次一个。

服务器类型

当你遵循 Nodal 的理念时,它的工作效果最好,这意味着创建一个新的服务来解决你的应用程序和业务的特定问题域。

主要的三个建议是 Branding Server、API Server 和 Application Server。

Nodal 的核心竞争力是建立 API 服务器。然而,我们也有一个名为 dotcom 的项目,用于构建 Branding Servers(搜索引擎优化的服务器生成的页面)。更多关于这方面的信息即将发布。

API 服务器

使用 Nodal 的模型、PostgreSQL 集成、内置的 JSON API 格式化和 Query Composer(ORM)创建一个 API 服务器。双向迁移是与 Nodal 打包的,这意味着你可以保持你的数据完整性。用户(包括密码)和 OAuth AccessToken 模型和控制器是为你预先建立的,可以很容易地添加到你的项目中。

与 Nodal 一起打包的还有工作者、调度模块,以及更多满足你所有数据需求的东西。

我们可以看看一个 API 控制器是什么样子的,比如说,博客文章。

class BlogPostsController extends Nodal.Controller {

  index() {

    BlogPost.query()
      .join('user')
      .join('comments')
      .where(this.params.query)
      .end((err, blogPosts) => {

        this.respond(err || blogPosts);

      });

  }

  show() {

    BlogPost.find(this.params.route.id, (err, blogPost) => this.respond(err || blogPost));

  }

  create() {

    BlogPost.create(params.body, (err, blogPost) => this.respond(err || blogPost));

  }

  update() {

    BlogPost.update(this.params.route.id, params.body, (err, blogPost) => this.respond(err || blogPost));

  }

  destroy() {

    BlogPost.destroy(this.params.route.id, (err, blogPost) => this.respond(err || blogPost));

  }

}

初学者指南

你可以在 nodaljs.com 上了解更多关于 Nodal 的信息。

文档

查看网站:nodaljs.com

路线图

ROADMAP.md 查看路线图。

关于

Nodal 正在积极开发,由 Keith Horwood 维护。

欢迎贡献者。

在 Twitter 上关注我,@keithwhor

在 GitHub 上叉叉我,keithwhor

感谢你对 Nodal 的关注!


(The first version translated by vz on 2021.07.10)

概覽

名稱與所有者keithwhor/nodal
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台Linux, Mac, Windows
許可證MIT License
發布數1
最新版本名稱v0.11.0 (發布於 2016-05-31 09:02:02)
第一版名稱v0.11.0 (發布於 2016-05-31 09:02:02)
創建於2015-04-19 06:34:05
推送於2023-01-16 02:03:22
最后一次提交2021-10-19 09:54:20
星數4.5k
關注者數108
派生數249
提交數831
已啟用問題?
問題數222
打開的問題數36
拉請求數94
打開的拉請求數2
關閉的拉請求數41
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Nodal

API Services Made Easy with Node.js

Build Status Join the chat at https://gitter.im/keithwhor/nodal

Nodal Logo

View the website at nodaljs.com.

Nodal is a web server and opinionated framework for building
data manipulation-centric (Create Read Update Destroy) API services in Node.js for
web, mobile or IoT apps.

Why Nodal?

Hello, Nodal — Building Node.js Servers for Everybody
is our first blog post that helps you get acquainted with the reasons behind
the creation of the framework. :)

Post Parse Prototyping is also a fantastic
read explaining the benefits of Nodal for quick and easy mobile / IoT backend development.

Overview

Nodal is built upon an ideology of a robust, scalable architecture for
data storage and retrieval APIs.
It is an opinionated, explicit, idiomatic and highly-extensible full-service
framework that takes care of all of the hard decisions for you and your team.
This allows you to focus on creating an effective product in a
short timespan while minimizing technical debt.

Nodal servers are not meant to be monoliths. They're stateless and distributed,
meant to service your needs of interfacing with your data layer effortlessly.
While you can output any data format with Nodal, it's recommended you offload
things like static page rendering to other optimized services like CDNs.

Check out the first Nodal Screencast here.

Stateless Dogma

It's important to note that Nodal is meant for stateless API services. This
means you should not rely on memory within a specific process to serve multiple
requests, and Nodal will use process clustering (even in development) to actively
discourage this practice. If you need to work with unstructured data for rapid
prototyping, connect Nodal to a PostgreSQL database and use the "JSON" field
type. You'll find yourself encountering a lot of trouble if you start trying to
use in-process memory across different requests.

Remember: one input, one output. Side effects dealing with model state
should be managed via your Database. Nodal should not be used for streaming
(long poll) requests and the HTTP request and response objects are intentionally
obfuscated.

This also means you can not rely on socket connections. If you need to
incorporate realtime functionality in your application, there should be a
separate server responsible for this. It can interface with your Nodal API
server and even receive events from it, but your API server should never have
a stateful (prolonged) connection with any client.

Getting Started

Getting started with Nodal is easy.

  1. Download and install the newest Node 6.x version from nodejs.org
  2. Open terminal, and type npm install nodal -g.
    (If you get an error, run sudo npm install nodal -g or fix permissions permanently by
    following these directions
  3. Using your terminal, visit your projects folder. Perhaps with cd ~.
  4. Run nodal new.
  5. Follow the on screen instructions, enter your new project directory and type nodal s.

That's it! Your Nodal webserver is up and running.

Hooking Up Your Database

Once Nodal is up and running, it's likely that you'll want to connect your project
to a database. Nodal comes packaged with Migrations, a Query Composer and full
PostgreSQL integration.

First you'll need to install PostgreSQL. OS X users, I recommend using
Postgres.app for your development environment.

Once you've installed Postgres, make sure to run:

$ createuser postgres -s

To create a default postgres superuser with no password. (Default for Nodal's
configuration.)

To begin using your database, start with:

$ nodal db:create

To create the database and then,

$ nodal db:prepare

To prepare for migrations.

From here, nodal db:migrate runs all pending migrations and nodal db:rollback
will roll back migrations, one at a time by default.

Server Types

Nodal works best when you follow its ideology, and that means creating a new
service to solve specific Problem Domains of your application and business.

The main three suggestions are Branding Server, API Server and Application Server.

Nodal's core competency is building API servers. We do, however, also have a
project called
dotcom for building Branding Servers
(search engine optimized server-generated pages). More on this soon.

API Server

Create an API server using Nodal's Models, PostgreSQL integration, built-in JSON
API formatting, and Query Composer (ORM). Bi-directional migrations are packaged
with Nodal, meaning you can maintain the integrity of your data.
User (including password) and OAuth AccessToken models and controllers are
pre-built for you and can be added easily to your project.

Packaged with Nodal are workers, scheduling modules, and much more for all of
your data needs.

We can look at what an API Controller might look like for, say, blog posts:

class BlogPostsController extends Nodal.Controller {

  index() {

    BlogPost.query()
      .join('user')
      .join('comments')
      .where(this.params.query)
      .end((err, blogPosts) => {

        this.respond(err || blogPosts);

      });

  }

  show() {

    BlogPost.find(this.params.route.id, (err, blogPost) => this.respond(err || blogPost));

  }

  create() {

    BlogPost.create(params.body, (err, blogPost) => this.respond(err || blogPost));

  }

  update() {

    BlogPost.update(this.params.route.id, params.body, (err, blogPost) => this.respond(err || blogPost));

  }

  destroy() {

    BlogPost.destroy(this.params.route.id, (err, blogPost) => this.respond(err || blogPost));

  }

}

Beginner's Guide

You'll be able to learn more about Nodal at nodaljs.com.

Documentation

Check out the website at nodaljs.com.

Roadmap

View the roadmap at ROADMAP.md.

About

Nodal is under active development and maintained by
Keith Horwood.

Contributors welcome!

Follow me on Twitter, @keithwhor

Fork me on GitHub, keithwhor

Thanks for checking out Nodal!

去到頂部