# @cretadoc/server

> The Cretadoc server.

Latest version **1.0.1** (published 2023-09-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @cretadoc/server
pnpm add @cretadoc/server
yarn add @cretadoc/server
bun add @cretadoc/server
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-09-04 |
| First published | 2023-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14.6.0 |
| Dependencies | 6 |
| Unpacked size | 68.5 KB |
| Known vulnerabilities | 0 (+12 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Armand Philippot |
| Maintainers | arphi |
| Keywords | cretadoc, server, express |

## Links

- npm: https://www.npmjs.com/package/@cretadoc/server
- Repository: https://github.com/ArmandPhilippot/cretadoc
- Homepage: https://github.com/ArmandPhilippot/cretadoc/tree/main/packages/server#readme
- Issues: https://github.com/ArmandPhilippot/cretadoc/issues
- npm.io page: https://npm.io/package/@cretadoc/server

## Dependencies (6)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [vite](https://npm.io/package/vite.md) ^4.4.9
- [helmet](https://npm.io/package/helmet.md) ^7.0.0
- [express](https://npm.io/package/express.md) ^4.18.2
- [@cretadoc/api](https://npm.io/package/@cretadoc/api.md) ^1.0.0
- [@cretadoc/utils](https://npm.io/package/@cretadoc/utils.md) ^1.0.1

## Recent versions

- 1.0.1 (latest) — 2023-09-04
- 1.0.0 — 2023-08-28

## README

# @cretadoc/server

It provides the server used by Cretadoc.

## Install

```
npm install @cretadoc/server
```

```
yarn add @cretadoc/server
```

```
pnpm add @cretadoc/server
```

## Usage

### With CommonJS

```cjs
const cretadoc = require('@cretadoc/server');

cretadoc.createServer().then((server) => {
  server.start();
  server.stop();
});
```

### With ESM

```mjs
import { createServer } from '@cretadoc/server';

const server = await createServer();
server.start();
server.stop();
```

## Configuration

The configuration object looks like:

```ts
export type ServerConfig = {
  /**
   * An API instance.
   * @default undefined
   */
  api: APIInstance | undefined;
  /**
   * The HMR configuration when using dev mode.
   * @default undefined
   */
  hmr: HMRConfig | undefined;
  /**
   * The server hostname.
   * @default "localhost"
   */
  hostname: string;
  /**
   * The server mode.
   * @default "development"
   */
  mode: 'development' | 'production';
  /**
   * The server port.
   * @default 3000
   */
  port: number;
  /**
   * The configuration to activate server-side rendering.
   * @default undefined
   */
  ssr: SSRConfig | undefined;
  /**
   * A configuration object to serve static files.
   * @default undefined
   */
  staticDir: StaticDirConfig | undefined;
};
```

### API configuration

The Cretadoc server can be used to serve an API instance. The `api` key accepts an instance of Cretadoc API (or a GraphQLYogaServer instance since we use an alias).

### HMR configuration

Under the hood, the server uses Vite for server-side rendering. You can change the default HMR port used by Vite. The `hmr` key accepts an object containing a `port` key which takes a number as value.

### Hostname configuration

By default, Cretadoc use `localhost` as hostname. You can use a different value if needed.

### Mode configuration

The mode is used to load different Express middleware in `production` and `development`. Default is: `development`.

### Port configuration

By default, Cretadoc uses the port `3000`. You can use a different value if needed.

### SSR configuration

The `ssr` key accepts an object as value with the following keys:

- `entrypoint`: A path to the server entrypoint that exports a render function.
- `route`: The route used for serve-side rendering (default is: `/`).

### Static directory configuration

You can use the Cretadoc server to serve a static directory. The `staticDir` key accepts an object as value with the following keys:

- `entrypoint`: A HTML file used as entrypoint (default is: `index.html`). It must be inside the given path.
- `path`: The (mandatory) static directory path.
- `route`: The route used to serve the static directory (default is: `/static`).

## Examples

These examples assume that you have already imported the `@cretadoc/server` package in the file. See the `Usage` section above.

### Serve a static directory at `/public` route.

```javascript
const server = await createServer({
  mode: 'production',
  port: 8080,
  staticDir: {
    entrypoint: 'main.html',
    path: '/home/username/sites/project/static/',
    route: '/public',
  },
});
```

### Use SSR to render the files.

```javascript
const server = await createServer({
  hmr: {
    port: 24569,
  },
  mode: 'development',
  port: 6000,
  ssr: {
    entrypoint: '/home/username/sites/project/entry-server.tsx',
  },
});
```

The `entry-server.tsx` file must export a `render` function containing the logic to render your HTML contents.

Example:

```tsx
import { StrictMode } from 'react';
import ReactDOMServer from 'react-dom/server';
import { App } from './app';

export const render = ({ res }) => {
  const htmlTemplate = readFileSync('template.html', 'utf8');
  const body = ReactDOMServer.renderToString(
    <StrictMode>
      <App />
    </StrictMode>
  );
  const html = htmlTemplate.replace('<!-- your-placeholder -->', body);

  res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
};
```

### Use the API route

```javascript
import { createAPI } from '@cretadoc/api';
import { createServer } from '@cretadoc/server';

const api = await createAPI();
const app = await createServer({ api });
```

## License

This package is released under the [MIT license](./LICENSE).

The documentation (Markdown files) is released under a [Creative Commons license](./LICENSE-docs).

---
_Source: https://npm.io/package/@cretadoc/server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
