-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
78 lines (68 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const memory = require('feathers-memory');
const winston = require('winston');
const path = require('path');
const PushNotifications = require('node-pushnotifications');
const log = winston.createLogger({
transports: [new winston.transports.Console()]
});
const settings = {
web: {
vapidDetails: {
subject: 'mailto@me.net',
publicKey: '< URL Safe Base64 Encoded Public Key >',
privateKey: '< URL Safe Base64 Encoded Private Key >'
},
gcmAPIKey:
'AAAA9U3NkO8:APA91bE9XnPD1WQGYle0dbJs6KO6HmAq_73f_z2PHoPr3sbRovXbn9q-PyJAKYlxgNI88rBpTzNWCXRuxhX9FcwbDBhT4h0BAiJr4BiEx8uSkWHXM92q04Q6cydK-dHGSnR20bsgH0Lg'
// TTL: 2419200
// headers: { }
// contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >'
}
};
const CLIENT_PATH = '/client';
const push = new PushNotifications(settings);
const app = express(feathers());
app.use(express.json());
app.configure(express.rest());
app.use(express.errorHandler());
app.use(CLIENT_PATH, express.static(path.join(__dirname, '../client')));
app.hooks({
before: {
all: context => {
log.info(
`Request to path ${context.path} with method ${context.method} ${
context.data ? 'and data ' + JSON.stringify(context.data) : ''
}`
);
}
},
error: context => {
log.error(
`Error in ${context.path} calling ${context.method} method`,
context.error
);
}
});
app.use('subscriptions', memory({}));
const subscriptionService = app.service('subscriptions');
app.use('push', {
async find(data, params) {
log.info('Sending push notifications to all known subscriptions');
try {
const subscriptions = await subscriptionService.find();
const results = await push.send(subscriptions, 'Push is working! :)');
log.info('Push results', results);
return results;
} catch (err) {
log.error('Unexpected error while sending web push notifications', err);
}
}
});
const SERVER_PORT = 3030;
app.listen(SERVER_PORT, () => {
log.info(
`Server started successfully. Demo Client http://localhost:${SERVER_PORT}${CLIENT_PATH}`
);
});