# Node, SSR và BFF

Tách secret backend và phiên từng người dùng. Node 24+ cho entry server.

Nguồn: https://whypik.com/docs/node-sdk
Nhóm: Lập trình viên
Phiên bản tài liệu: 2026-10-14.10
Ngôn ngữ: vi

## Server Data API

Liên kết mục: https://whypik.com/docs/node-sdk#section-0

Server key cũ vẫn dùng policy server:read/server:write. Khi cần toàn quyền dữ liệu và cấu trúc của Project trong backend tin cậy, tạo khóa Admin riêng và dùng WhyPikAdmin/AdminClient theo bài Admin SDK. Không tự đổi khóa cũ thành Admin.

```
import { WhyPikServer } from "@whypik/js/server";
const database = new WhyPikServer({
  project: process.env.WHYPIK_PROJECT,
  key: process.env.WHYPIK_DATA_SECRET,
  backendOrigin: "https://app.whypik.com"
});
const rows = await database.from("versions").limit(20).select();
// Khi shutdown: database.destroy();
```


## Auth cookie trên website ngoài WhyPik

Liên kết mục: https://whypik.com/docs/node-sdk#section-1

Tạo BFF key ở Authentication gắn chính xác origin app. Mount createAuthProxy trước SPA/static. Framework chuyển request/response chuẩn Web API và giữ mọi Set-Cookie, no-store. clientAddress lấy từ socket hoặc ingress đã xác minh, không tin header tùy ý của browser.

```
import { createAuthProxy } from "@whypik/js/server";
const handleAuth = createAuthProxy({
  project: process.env.WHYPIK_PROJECT,
  publicKey: process.env.WHYPIK_PUBLIC_KEY,
  backendKey: process.env.WHYPIK_BFF_KEY,
  backendOrigin: "https://app.whypik.com",
  applicationOrigin: "https://your-app.example",
  clientAddress: request => trustedRequestAddresses.get(request)
});
// Framework: route /.whypik/auth/* -> await handleAuth(request).
// trustedRequestAddresses do adapter server điền từ transport.
```


## Browser dùng BFF

Liên kết mục: https://whypik.com/docs/node-sdk#section-2

Auth mặc định /.whypik/auth. Adapter trên chỉ proxy Auth; Data của website ngoài cần endpoint tuyệt đối WhyPik như dưới. Origin app phải allowlist.

```
const client = new WhyPik({project, key,
  data:{endpoint:`https://app.whypik.com/api/v1/data/${project}`}
});
```


## SSR và tài khoản

Liên kết mục: https://whypik.com/docs/node-sdk#section-3

Không giữ client đã đăng nhập trong singleton toàn process. Tạo client theo request nếu dùng user session ở server, hủy sau request. WhyPikServer cho công việc backend cố định không đại diện user; tự kiểm tra user app của bạn trước mọi hành động backend theo yêu cầu user.


## Đọc tiếp

- [Cài SDK và chọn đúng loại](https://whypik.com/docs/sdk-install)
- [Auth SDK: đăng ký, phiên và khôi phục](https://whypik.com/docs/auth-reference)
- [REST Data API](https://whypik.com/docs/data-rest)
- [Admin SDK: Database của Project](https://whypik.com/docs/data-admin)
