Web Application using Express (expressjs.com) Modules for Compass Test Automation, One of Libraries in CTA-OSS Framework
In this module, cta-expresswrapper, we're implementing cta-tool using Express as web application.
We aim to give you brief guidelines here.
'use strict';
const ExpressWrapper = require('cta-expresswrapper');
const instance = new ExpressWrapper(dependencies, configuration);
instance.start();
We use ExpressWrapper.start() to start the Express.
In cta-expresswrapper, the ExpressWrapper class is extending Tool from cta-tool and is exported.
'use strict';
const Tool = require('cta-tool');
class ExpressWrapper extends Tool {
...
}
module.exports = ExpressWrapper;
Because the ExpressWrapper class is extending Tool, its constructor has dependencies and configuration as parameters.
class ExpressWrapper extends Tool {
constructor(dependencies, configuration) {
...
super(dependencies, configuration);
...
}
}
ExpressWrapper uses configuration via constructor.
class ExpressWrapper extends Tool {
constructor(dependencies, configuration) {
...
super(dependencies, configuration);
...
}
}
Because of Tool (cta-tool) extension, the configuration needs to have these fields.
- name - define the tool name
- singleton - indicate whether the tool is singleton
- properties - provide properties
ExpressWrapper uses port (number) in properties. Here is an example:
const configuration = {
name: 'my-express',
module: 'cta-expresswrapper',
properties: {
port: 3000,
},
};
In ExpressWrapper class, it provides start() to use Express.listen(port).
const express = require('express');
const http = require('http');
class ExpressWrapper extends Tool {
constructor(dependencies, configuration) {
...
this.app = express();
this.server = http.createServer(this.app);
this.port = this.properties.port;
...
}
start() {
...
this.server.listen(this.port);
...
}
}
- Should we specify the version of Express in package.json because it's dependency?