@kth/kth-node-web-common v9.3.1
@kth/kth-node-web-common
Changes in version 8
First, make sure your code is up to date with Changes in version 6
The error page can now be rendered directly from inside the package. No need to copy and register the Handlebars-file from the app.
If the app only renders the error page via the renderErrorPage
helper, you can remove lines like below:
mkdir -p ./server/views/system ./server/views/layouts
cp ./node_modules/@kth/kth-node-web-common/lib/handlebars/pages/views/ ...
cp ./node_modules/@kth/kth-node-web-common/lib/handlebars/pages/layouts/ ...
They are usually located in build.sh
, or in the scripts section of package.json
⚠️ Warning it will still be possible to copy the handlebar files to your applications, but they will no longer be updated, and will probalby be removed in the future. ⚠️
Handlebar Helpers
This is a set of standard helpers needed in most KTH node-web projects.
Register helpers:
const registerHeaderContentHelper = require('@kth/kth-node-web-common/lib/handlebars/helpers/headerContent')
registerHeaderContentHelper({
proxyPrefixPath: '/app/mount/point',
version: 'x.x.x'
})
Usage in templates:
- {{ withVersion url }} -- appends
'?v=' + version
to the passed string - {{ extend name options }} -- calls the function options.fn with content of block name as param and adds it to named block
- {{ prefixScript url name }} -- add a script tag with version set to script block name
- {{ prefixStyle url name media }} -- add a style tag for named media type with version set to style block name
- {{ render name }} -- used by a layout to render script and style blocks in appropriate places
Cortina Blocks
Express middleware to fetch Cortina CMS blocks for requests with layouts requiring them:
Uses https://github.com/kth/cortina-block
route.use('/app/mount/point', require('@kth/kth-node-web-common/lib/web/cortina')({
blockUrl: 'https://url.to/fetch/block',
headers: { // Optional way of passing headers to kth-node-cortina-block request
'User-Agent': 'something...'
},
addBlocks: { // Optional way of adding Cortina blocks on top of defaults
menu: '1.678435'
},
proxyPrefixPath: '/app/mount/point',
hostUrl: 'http://server_host:port',
redisConfig: { ... }, // Optional redis config object, see kth-node-configuration.
globalLink: true, // Default false if not set, if true the language link point to the startpage of KTH,
supportedLanguages: ['sv'], // Optional - set to languges supported - if only one language is supported, globalLink sets to true
siteNameKey = 'site_name', // Defaults to site_name. This key need to be set in i18n messages file
localeTextKey = 'locale_text', // Defaults to locale_text. This key need to be set in i18n messages file
}))
Crawler Redirect
Middleware to handle redirects for crawlers.
const excludePath = '/app/mount/point' + '(?!/static).*'
const excludeExpression = new RegExp(excludePath)
server.use(excludeExpression, require('@kth/kth-node-web-common/lib/web/crawlerRedirect')({
hostUrl: 'http://sertver_host:port',
}))
Language
Middleware and helper methods to set and get language for this request.
Register the middleware:
const { languageHandler } = require('@kth/kth-node-web-common/lib/language')
sever.use('/app/mount/point', languageHandler)
In a controller you can get the language using the helper method getLanguage:
const language = require('@kth/kth-node-web-common/lib/language')
const lang = language.getLanguage(res)
Error Page
There is a helper for rendering error pages with proper styling
To use it, the following must be configured:
Then, import the helper and use as a final method to express
const errorHandler = require('kth-node-web-common/lib/error')
// commonly found in systemCtrl.js
function _final(err, req, res, next) {
errorHandler.renderErrorPage(res, req, statusCode, i18n, isProd, lang, err)
}
Changes in version 6
Here is a small migration guide if your application is build from the node-web template.
Please note when using version 7, the package name must be changed to @kth/kth-node-web-common.
Include common error messages from the package into your application
In your application, add the following into your i18n/index.js
// Include error messages from kth-node-web-common package
const errorMessagesEnglish = require('kth-node-web-common/lib/i18n/errorMessages.en')
const errorMessagesSwedish = require('kth-node-web-common/lib/i18n/errorMessages.sv')
// Include application messasges
const messagesEnglish = require('./messages.en')
const messagesSwedish = require('./messages.se')
// Add the error messages to the application defined messages before pushing them.
messagesSwedish.messages = { ...messagesSwedish.messages, ...errorMessagesSwedish.messages }
messagesEnglish.messages = { ...messagesEnglish.messages, ...errorMessagesEnglish.messages }
Use the function from the kth-node-web-common
package in the apllication systemCtrl.js
Do the following changes in your /server/controller/systemCtrl.js
- Include the error handler
const errorHandler = require('kth-node-web-common/lib/error')
- Remove the
\_getFriendlyErrorMessage
method. - In the
\_final
method, add the following code after the declaration oflang
variable:
// Use error pages from kth-node-web-common based on given parameters.
errorHandler.renderErrorPage(res, req, statusCode, i18n, isProd, lang, err)
- Remove the switch and
res.format
code. - Remove unused error labels the messages files
/i18n/messages.en.js
and/i18n/messages.sv.js
4 days ago
4 days ago
10 days ago
1 month ago
1 month ago
2 months ago
2 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
9 months ago
9 months ago
10 months ago
8 months ago
8 months ago
6 months ago
7 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago