Docs-server
A server implementation which is used to build a docs system.
Why
I create this project just for implementing my docs back-end simply. It's unusual back-end module which is used to build disposable micro-server.
I use this project in some platform where user has no write permission on runtime mode, so this project don't support for adding new docs when server is running. This is intentional. You can use this project to your own documentation module If you have same situation.
Features
-
Perform automatic markdown searching and generate correct dynamic routes according to the root path of your project.
-
Support multiple-level documentation routes.
-
Support for specifying additional static resources routes.
-
Support for customizing all docs routes
-
Support for customizing docs response headers
-
Support for specifying minimum response size in bytes to turn on gzip
-
Support static
mode, You can only generate JSON
static files without server building.
Installing
yarn add docs-server
npm i docs-server
Usage
- Building server from easy way
const DocsServer = require('docs-server')
const app = new DocsServer()
Server mode
You can specify your custom configuration.
const resolve = require('path').resolve
const DocsServer = require('docs-server')
const app = new DocsServer({
cwd: resolve(__dirname, './'),
dest: resolve(__dirname, './menu.json'),
port: '8800',
headers: {
'Access-Control-Allow-Origin': '*',
'vary': 'origin',
'Access-Control-Allow-Methods': 'GET,POST'
},
threshold: 1
extra: [
{
route: '/test',
middleware: async (ctx, next) => {
}
}
],
filter: (origin) => {
const removeShortDate = origin.replace(/\/{0}(\d{6}-)+/g, '')
const removeInitialYear = removeShortDate.replace(/^\d{4}/, '')
const removeRepeat = removeInitialYear.replace(/^\/\S+\//, '')
const removeExtension = removeRepeat.replace(/\.md$/, '')
return `writings/${removeExtension}`
},
headerMiddleware: async function (ctx, next) {
const isInWhitelist = ctx.origin === 'https://github.com'
|| ctx.origin === 'http://example.com'
if (isWhitelist) {
ctx.set({
'Access-Control-Allow-Origin': `${ctx.origin}`
})
}
await next()
}
})
-
Notice:
-
All options is optional
-
Default filter will just remove docs file extension name
-
Custom filter MUST return a string type value, and it will be only used to generate docs routes (excluding extra routes
and menu.json
)
-
Two choices to set response headers
-
headers: set a headers object that will be used to set response header
-
headerMiddleware: default middleware will be replaced by your headerMiddleware setting, and DON'T forget invoke next() in middleware function body
-
Test your building
# test your server
curl -v http://localhost:8800 # response from /
curl -v http://localhost:8800/doc/sample # response from /doc/sample
Static mode
const DocsServer = require('docs-server')
new DocsServer({
mode: 'static',
staticNormalize (origin) {
return origin.replace(/\.md$/, '')
}
})
In the static mode, mode
option is required, and staticNormalize
is optional. staticNormalize
option is used to specify what you want to change based on markdown file path.
The sample file under static mode is here
CHANGELOG
CHANGELOG