You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copyright (c) 2019 Cognitive and Immersive Systems Lab
4
4
5
5
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
7
7
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
8
9
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
An opinionated wrapper around the popular [express](https://expressjs.com/)library as
4
-
well as [socket.io](https://socket.io/).
3
+
An opinionated wrapper around the popular [express](https://expressjs.com/)and [websockets/ws](https://github.com/websockets/ws) libraries. It assumes that you are running
4
+
an application with a `cog.json` file.
5
5
6
6
The library assumes that you want to use `cookie-parser`, `body-parser` (namely JSON),
7
7
`json spaces` set to 2, and use `ejs` as the view engine. Finally, the port is
@@ -12,50 +12,71 @@ used from the `cog.json` file.
12
12
npm install @cisl/express
13
13
```
14
14
## Usage
15
+
In straight JS:
15
16
```js
16
17
constexpress=require('@cisl/express');
17
-
// or typescript
18
-
importappfrom'@cisl/express';
18
+
constapp=express();
19
+
app.listen();
20
+
```
19
21
22
+
or in TypeScript:
23
+
```typescript
24
+
// or typescript
25
+
importexpressfrom'@cisl/express';
20
26
const app =express();
27
+
21
28
app.listen();
22
29
```
30
+
The `listen` method above does not accept any parameters, and will automatically use the
31
+
port specified in the `cog.json` file that should exist in current working directory when
32
+
running the above.
23
33
24
-
and then it can be used the same as a regular express object:
34
+
After creating the object, it can be used as a regular express app:
25
35
```js
26
36
app.get('/', (req, res) => {
27
37
res.json({'msg':'Hello World'});
28
38
});
29
39
```
30
40
31
-
as well as accessing the attached socket.io instance:
41
+
as well as accessing the attached WebSocket server:
32
42
```js
33
-
app.socketio.on('connection', (socket) => {
43
+
app.wsServer.on('connection', (socket) => {
34
44
console.log('a user connected');
45
+
socket.on('message', data=> {
46
+
console.log(`Received: ${data}`);
47
+
socket.send('pong');
48
+
});
35
49
});
36
50
```
37
51
38
-
If you want to disable the socket.io instance, pass in `{socketio: false}` to the `express`
39
-
function.
52
+
The available objects off the original (in addition to the normal express stuff) is:
53
+
*`expressListen` - original express listen method, should largely not be necessary/used over `listen()`
54
+
*`httpServer` - the underlying `HttpServer` instance
55
+
*`wsServer` - the underlying `WebSocket.Server` instance
40
56
41
-
If you need the original listen method, it can be accessed through `app.expressListen`.
42
-
Socket.io is expressed through `app.socketio`.
43
57
44
-
Additionally, the package will automatically add a `/test` GET route that returns
58
+
Finally, the package will automatically add a `/test` GET route that returns
45
59
a JSON object with the following definition:
46
60
```json
47
61
{
48
62
"response": "AOK",
49
63
"error": null
50
64
}
51
65
```
52
-
which can be used as a small healthcheck for these applications.
66
+
which can be used as a small healthcheck for apps running `@cisl/express`.
53
67
54
68
## Configuration
55
69
Using this package assumes you have a `cog.json` file with at least the following
56
70
in it:
57
71
```typescript
58
72
{
59
-
"port": string|number
73
+
"port": number
60
74
}
61
75
```
76
+
77
+
## Output
78
+
`@cisl/express` will only output a single line when you run the
79
+
`listen()` command with the following message `@cisl/express listening on port <PORT>`.
80
+
This will be output as a standard `console.log`, or if you have `@cisl/logger` installed,
81
+
using `logger.info`. You can silence the output, by sending `{quiet: True}` to the
0 commit comments