# node-metas

> Node Metas

Latest version **0.1.16** (published 2020-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-metas
pnpm add node-metas
yarn add node-metas
bun add node-metas
```

Provides the command `node-metas`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.16 |
| Published | 2020-06-01 |
| First published | 2020-05-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 37.1 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | mail@nicomartin.ch |
| Maintainers | nico_martin |

## Links

- npm: https://www.npmjs.com/package/node-metas
- Repository: https://github.com/nico-martin/node-metas
- Homepage: https://github.com/nico-martin/node-metas#readme
- Issues: https://github.com/nico-martin/node-metas/issues
- npm.io page: https://npm.io/package/node-metas

## Dependencies (5)

- [esm](https://npm.io/package/esm.md) ^3.2.25
- [http](https://npm.io/package/http.md) ^0.0.1-security
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.3
- [node-static](https://npm.io/package/node-static.md) ^0.7.11
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.1.0

## Recent versions

- 0.1.16 (latest) — 2020-06-01
- 0.1.15 — 2020-05-31
- 0.1.14 — 2020-05-31
- 0.1.13 — 2020-05-31
- 0.1.12 — 2020-05-31
- 0.1.11 — 2020-05-31
- 0.1.10 — 2020-05-31
- 0.1.9 — 2020-05-31
- 0.1.8 — 2020-05-31
- 0.1.7 — 2020-05-31
- 0.1.6 — 2020-05-31
- 0.1.5 — 2020-05-31
- 0.1.4 — 2020-05-31
- 0.1.3 — 2020-05-24
- 0.1.2 — 2020-05-24
- … 8 more at https://npm.io/package/node-metas/versions

## README

# Node Metas

> **Note:** This is a very early preview. So right now I expect to introduce breaking changes with each release.

"Node Metas" is a NodeJS library that starts a production ready NodeJS Webserver, serves a specific dir and allows you to manipulate the meta headers inside the index.html.

This is super useful if you only need to add server-side rendered meta tags but you don't want to render the whole app serverside.

## Yes, but why ?!?
I had to use NextJS in a bunch of projects and it's not that I don't like next, I'm just not a big fan of the strict structure. Your whole app needs to follow the NextJS logic for the server-side renderer to work. But in a lot of cases I only really needed it for some meta tags (for example for social media sharing). That's why this library follows a much simpler approach. This being said it works with any kind of framework. It just takes the public dir, serves the files and adds meta tags for routes that are defined under "routes".
## API

At the core this lib provides the possibility to define routes and to pass a function (sync or async) that returns an object of { name: content } meta tags.
```js
{
  path: '/',
  metas: request => {
    return {
      'name-of-the-meta': 'content of the meta',
      url: '/',
    }
  },
}
```

### Full example

```js
import nodeMetas from './lib/index';
import fetch from 'node-fetch';

nodeMetas({
  routes: [
    {
      path: '/user/:id/',
      metas: request => ({
        'user-id': 'id' in request.params ? request.params.id : '',
        hello: 'world',
      }),
    },
    {
      path: '/post/:id/',
      metas: async request => { // can be async
        const id = 'id' in request.params ? request.params.id : 0;
        let metas = {};
        try {
          const resp = await (
            await fetch(`https://sayhello.ch/wp-json/wp/v2/posts/${id}/`)
          ).json();
          metas = {
            title: resp.title.rendered,
          };
        } catch (error) {
          console.log('Error for /post/:id/');
        }
        return metas;
      },
    },
  ],
  port: 3000, // optional
  indexFile: 'index.gtml', // optional
  serveDir: 'dist/', // optional
});
```

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