UNPKG

3.37 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | ewd-document-store: Persistent JavaScript Objects and Document Database |
5 | using Global Storage |
6 | |
7 | Copyright (c) 2017 M/Gateway Developments Ltd, |
8 | Reigate, Surrey UK. |
9 | All rights reserved. |
10 | |
11 | http://www.mgateway.com |
12 | Email: rtweed@mgateway.com |
13 | |
14 | |
15 | Licensed under the Apache License, Version 2.0 (the "License"); |
16 | you may not use this file except in compliance with the License. |
17 | You may obtain a copy of the License at |
18 | |
19 | http://www.apache.org/licenses/LICENSE-2.0 |
20 | |
21 | Unless required by applicable law or agreed to in writing, software |
22 | distributed under the License is distributed on an "AS IS" BASIS, |
23 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
24 | See the License for the specific language governing permissions and |
25 | limitations under the License. |
26 ----------------------------------------------------------------------------
27
28 7 September 2017
29
30*/
31
32// Demonstrates use of CacheModule.js worker module which connects each
33// worker process to the Cache database, intergrated with Express
34
35// Start using:
36// $ node examples/CacheExpress.js
37// You may need to run this as sudo due to Cache permissions
38
39'use strict';
40
41require('dotenv').config();
42
43var express = require('express');
44var bodyParser = require('body-parser');
45var qoper8 = require('ewd-qoper8');
46var qx = require('ewd-qoper8-express');
47
48var app = express();
49app.use(bodyParser.json());
50
51var q = new qoper8.masterProcess();
52qx.init(q);
53
54app.post('/qoper8', function (req, res) {
55 q.handleMessage(req.body, function (resultObj) {
56 delete resultObj.finished;
57 res.send(resultObj);
58 });
59});
60
61app.get('/', function (req, res) {
62 res.sendFile(__dirname + '/index.html');
63});
64
65q.on('start', function () {
66 this.worker.module = process.cwd() + '/examples/CacheModule';
67});
68
69q.on('started', function () {
70 var server = app.listen(8080, function () {
71 var host = server.address().address;
72 var port = server.address().port;
73
74 console.log('EWD-Express listening at http://%s:%s', host, port);
75 console.log('__dirname = ' + __dirname);
76 });
77
78 var io = require('socket.io')(server);
79 io.on('connection', function (socket) {
80 socket.on('my-request', function (data) {
81 q.handleMessage(data, function (resultObj) {
82 delete resultObj.finished;
83 socket.emit('my-response', resultObj);
84 });
85 });
86 });
87});
88
89q.start();