@cat-org/server v1.5.8
@cat-org/server ·
Run koa server with pipeline
and babel
. This package is just a helper. You can replace any method in @cat-org/server
by using the custom functions.
Install
yarn add koa @cat-org/server
Run command
This command is base on babel
. You can use any babel
command expect for yarn server ./src/server.js
.
If you want to use the default server
, you need to have a soruce folder. For example:
yarn server src -d lib
If you want to use the custom server
, you need to add a custom server file, For example:
import server form '@cat-org/server';
export default () =>
server.init()
|> server.run()
And run with:
yarn server ./src/server.js -o ./lib/server.js
Environment variables
PORT
: Set the port of the serverNODE_ENV
: Set the dev mode or not.
Use default server in testing
import server from '@cat-org/server/lib/defaults';
let runningServer;
describe('server', () => {
beforeAll(async () => {
runningServer = await server({
src: './path-to-src-folder',
dir: './path-to-dir-folder',
});
});
afterAll(() => {
runningServer.close();
});
});
Write the custom server
import server from '@cat-org/server';
export default ({ src, dir, dev, watch, port }) =>
server.init()
|> server.use(async (ctx, next) => {
await next();
})
|> ('/path' // if this is undefined, this will not add prefix to router
|> server.start
|> ('/get'
|> server.get // this will render as /path/get with get method (post, put, del, all)
|> server.use(async (ctx, next) => {
await next();
})
|> server.end)
|> server.use(async (ctx, next) => {
await next();
})
|> server.end)
|> (undefined
|> server.start
|> ('/get' // this will render as /get with get method
|> server.get
|> server.end)
|> server.end)
|> server.run()
|> server.watch(dir, []);
init
: Just use to returnKoa
. You can replace bynew Koa()
.use
: Add middleware to the router or the server.start
,end
: Add a new router.get
,post
,put
,del
,all
: Add method to this router.run
: Just use to return a promise withnew Koa().listen(port)
. The default of the port is 8000.watch
: Use this function to watch the changed file. This method is based onchokidar
. You can write the same function by yourself. If you want to add the function to handle the changed file, you can add an array in the second argument.
Load plugins with default server
You can add those packages in your project and @cat-org/server
will load the default config for those modules.
@cat-org/koa-base
@cat-org/koa-graphql
@cat-org/koa-react
@cat-org/use-css
@cat-org/use-less
You only need to do this:
yarn add @cat-org/package-name
Environment variables about those plugins
- Run command without building js in the production mode.
SKIP_BUILD=true NODE_ENV=production yarn server src -d lib
- Run command without running relay-compiler.
SKIP_RELAY=true yarn server src -d lib
- Run command without running server.
SKIP_SERVER=true yarn server src -d lib
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago