4.7.0 • Published 5 years ago

nocms-server v4.7.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

NoCMS Server

Web server for NoCMS sites.

semantic-release Dependency Status devDependencies

API

init(config)

Initializes the server with config values.

import nocmsServer from 'nocms-server';

const configObj = {
  port: 8000,
  pageService: 'http://localhost:8001',
  i18nApi: 'http://localhost:8002',
  tokenSecret: 'shhhhhh',
};

nocmsServer.init(configObj);

Available config values

FieldDescriptionDefault
portPort of which the server is listening.3000
tokenSecretSecret for verifying user tokens. Required for publishing features''
pageServiceURL for accessing page service. Required for nocms solutionsnull
loggerLogger function, supporting debug, warn, info and errorconsole
i18nApiURL for accessing i18n servive. Required for translation featuresnull
languageListArray of languages availabke to pages.[]
assetsFolderPath to folder containing static files, relative to process dirassets
assetsBasePathAssets mount path pointing to where the files can be requested by the browser./assets
clientAppScriptPath to the client application part of the universally rendered pages/assets/js/nocms.js
commonAppScriptIf the client script is built with a commons chunk, it can be put here.null
adminAppScriptPath to the admin interface application script/assets/js/admin.js
adminAppCssPath to CSS file for the admin interface/assets/css/admin.css
includeMainCssFlag for controlling if css should be included in the generic page outputtrue
mainCssPath to the main css file/assets/css/main.css
verboseFlag for controlling verbose logging outputfalse
compressResponsesFlag for controlling if responses should be compressed or nottrue

addRedirects(redirectsArr)

Add an array of redirects on the form, [{ from: '<source>', to: '<target>' }, ... ].

nocmsServer.addRedirects([{ from: '/foo', to: '/bar' }])

When the user tries to access /foo the response ends with status 301 and Location: /bar-header is sent.

addRedirect(to, from)

Adds a single redirect to the redirect list. nocmsServer.addRedirect('/foo', '/bar') is equivalent to the above example.

addDataSource(urlPattern, handlerFunc)

Although NoCMS Server uses a page datasource by default, you can override the datasource by using the addDataSoruce function.

const peopleDataSource = (nocms) => {
return new Promise((resolve) => {
    nocms.pageData = {
      templateId: 'my-template',
      pageTitle: 'Tom is a great guy',
      uri: '/people/tom',
      // ...
      // other page data
      // ...
    };
    resolve(nocms);
  });
};
nocmsServer.addDataSource('/people/*', peopleDataSource);

Instead of requesting the page data service, page data are resolved using the peopleDataSource function for urls matching /people/*. It is important that the pageData object assigned to nocms.pageData contains the following fields: uri, templateId and pageTitle as these are used by the included nocmsServer components.

addComponentDataSource(componentType, handlerFunc)

// TODO

addSites(sites)

Add site configuration for the server. If no site configuration is added, server will default to site named localhost with language en on all domains. If the sites run on multiple domains, you can specify which domains should be resolbed to which sites. The Host header is used for site resolving.

const sites = [
  {
    "name": "example-en",
    "domains": ["example.com", "xmpl.com"],
    "lang": "en"
  },
  {
    "name": "example-no",
    "domains": ["example.no"],
    "lang": "no"
  }
];
nocmsServer.addSites(sites);

If you access the server with http://xmpl.com in the example, the site is resolved to example-en with language en.

setDefaultSite(name)

Set which site the requests that don't match any domain should be resolved to. The function takes the name of the site as an argument.

nocmsServer.setDefaultSite('example-en');

addMiddleware(name, url, middlewareFunc)

You can add your own middleware to execute custom operations at request scope. This is useful for adding custom tracking, applying a content security policy (CSP), check- or set cookies, etc. The middleware takes a name argument, which is for convenience, an optional url argument, which filters which URLs the middleware should be applied to, and the middleware function which is a connect compliant function.

const middleware = (req, res, next) => {
	res.append('X-Was-Here', 'Jørgen');
	next();
};

nocmsServer.addDataSource('Jørgen was here', '*', middleware);

setAreas(areas)

When NoCMS Server receives a request for an HTML page, the server renders a react component named Page. The component includes the <html>, <head> and <body> elements, and within the body tag, the requested template is rendered. In addition, there are certain fixed areas that are outputted if they are provided. These areas can be set using the setAreas function.

Available areas are:

  1. headContent: Last in the head element
  2. topContent: First in the top in the body element
  3. bottomContent: After the #mainContent element
  4. script: Last in the body element, useful for placing custom javascript files in

Note that headContent needs to be a react fragment, as the head element doesn't support nesting of elements. ( <> <meta name="foo" content="foo" /> </>)

const areas = {
  topContent: (pageData) => { return <p>{pageData.title}: Put your top content here</p>; },
  bottomContent: <p>Put your bottom content here</p>,
  headContent: <><meta name="was here" content="Jørgen" /></>,
};

nocmsServer.setAreas(areas);

setTemplates

This function is used to pass in the templates for the different pages of the site. A template has an id which should be a human readable identificator and a component which is the react component that are eventually rendered into the #mainContent element.

If the template are supposed to be available in the ""Create Page" dialog in the NoCMS Publishing interface, it would also need to have set a flag named siteTemplate and a name object containing a field for each admin interface language. Finally it could have an array of section components which will be available for inclusion in the "Add Component" dialog in NoCMS Publishing.

import Article from './Article.jsx';

const templates = [
  {
    id: 'article',
    component: Article, // Do not use jsx like <Aricle /> here, as it will be rendered later on.
    name: { // Translations for each admin interface language
      no: 'Artikkel',
      en: 'Article',
    },
    siteTemplate: true,
    sections: [
      {
        name: 'text', // section identificator
        description: 'Plain rich text component', // component description, visible in NoCMS Publishing
        categories: ['newsAndBlogpost', 'all'], // available in these groups in the NoCMS Publishing, Add Component interface.
        icon: '', // optional icon from "material icons" that is associated with the section component
        label: 'News',
        defaultData: { content: '...' },
      }
    ],
  },
  {
    id: 'custom-data-soruce-template',
    component: CustomTemplateComponent,
  },
]

nocmsServer.setTemplates(templates);

Page data is passed on to the template components using the spread operator, meaning that pageData.pageTitle will be available as props.pageTitle.

setRobotsTxt

You can set the robots.txt file by pointing to a file relative to the project root and process working directory. The content will be returned for /robots.txt requests

nocmsServer.setRobotsTxt('files/robots.txt');

start

This function starts the party by initializing the middlewares and passing on configuration values to them, and finally starting the server.

Middleware

#NameDescriptionurl pattern
1assetsServing static filesconfig.assetsBasePath/assets
2defaultFaviconHandlerHandling default favicon requests */favicon.ico
3robotsTxtHandlerServing robots.txt file content/robots.txt
4correlatorMiddleware for assigning correlation id to requesst, using nocms-express-correlation-id*
5healthEndpoint for health monitoring tools using express-healthcheck/health
6metricsMiddleware for recording request metrics using Prometheus and nocms-express-metrics*
7prepareMiddleware for preparing request for request pipeline*
8cookieParserExpress cookie-parser*
9redirectTrailingSlashRequestsUrls ending with / are redirected. /foo/ is redirected to /foo*
10redirectsIf url matches a redirect (using addRedirects), location header are sent and response is completed.*
11siteResolverSetting site and lang on res.locals based on host header.*
12nocms-authVerifying and reading tokens. Populating claims field on res.locals.*
13nocms-reauthIf access token are expired, but refresh token are valid, user is redirected to /api/login/refresh*
14clearCacheMiddlewareMiddleware for clearing entire Varnish cache. Requires publisher claim*
15externalMiddlewaresPer project custom middleware added using addMiddleware.*
16compressionCompress responses using compression package.*
17requestHandlerRequest pipeline middleware*
18errorHandlerError handler middleware catching exceptions and providing error responses*

(*) Browsers try to find favicon at /favicon.ico, but favicon is specified in Page.jsx to be found at /assets/favicon.ico

Request pipeline

Page Data Source

Universal rendering

React Components

Page Component

NoCMSClient Component

MainContent Component

Commit message format and publishing

This repository is published using semantic-release, with the default AngularJS Commit Message Conventions.

4.7.0

5 years ago

4.6.2

5 years ago

4.6.1

5 years ago

4.6.0

6 years ago

4.5.4

6 years ago

4.5.3

6 years ago

4.5.2

6 years ago

4.5.1

6 years ago

4.5.0

6 years ago

4.4.0

6 years ago

4.3.0

6 years ago

4.2.3

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.5

6 years ago

4.1.4

6 years ago

4.1.3

6 years ago

4.1.2

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.5.0

6 years ago

3.4.7

6 years ago

3.4.6

6 years ago

3.4.5

6 years ago

3.4.4

6 years ago

3.4.3

6 years ago

3.4.2

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.4

6 years ago

3.3.3

6 years ago

3.3.2

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.11.1

6 years ago

2.11.0

6 years ago

2.10.0

6 years ago

2.9.0

6 years ago

2.8.3

6 years ago

2.8.2

6 years ago

2.8.1

6 years ago

2.8.0

6 years ago

2.7.0

6 years ago

2.6.0

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago