-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
181 lines (154 loc) · 4.78 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
var moesifExpress = require('moesif-express');
var superagent = require('superagent');
// Set the options, the only required field is applicationId.
var moesifOptions = {
applicationId: process.env.MOESIF_APPLICATION_ID || 'Your Moesif Application Id',
debug: false,
identifyUser: function (req, res) {
if (req.user) {
return req.user.id;
}
if (req.headers['my-user-id']) {
return req.headers['my-user-id'];
}
return undefined;
},
identifyCompany: function (req, res) {
if (req.headers['my-company-id']) {
return req.headers['my-company-id'];
}
return undefined;
},
getSessionToken: function (req, res) {
return req.headers['Authorization'];
},
getMetadata: function (req, res) {
return {
foo: 'express',
bar: 'example'
}
},
disableBatching: true,
logBody: true,
callback: function (error, data) {
console.log('inside call back');
console.log('error: ' + JSON.stringify(error));
}
};
// Creates an ExpressJS compatible Feathers application
const app = express(feathers());
var moesifMiddleware = moesifExpress(moesifOptions);
app.use(moesifMiddleware);
moesifMiddleware.startCaptureOutgoing();
app.get('/', function (req, res) {
res.json({ message: 'first json api'});
});
app.get('/outgoing/posts', function(req, res) {
console.log('outgoing is called');
superagent.get('https://jsonplaceholder.typicode.com/todos/2').then(function (response) {
console.log('back from outoging');
console.log(response.body);
res.json({ fromTypicode: response.body });
}).catch(function(err) {
res.status(500).json(err);
});
});
app.post('/users(/:userId)', function(req, res) {
// updateUser can be called anywhere in the node
// this is just an example it can be easily triggered by the
// test script.
// Only userId is required.
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
// metadata can be any custom object
var user = {
userId: req.params.userId,
companyId: '67890', // If set, associate user with a company object
campaign: {
utmSource: 'google',
utmMedium: 'cpc',
utmCampaign: 'adwords',
utmTerm: 'api+tooling',
utmContent: 'landing'
},
metadata: {
email: 'john@acmeinc.com',
firstName: 'John',
lastName: 'Doe',
title: 'Software Engineer',
salesInfo: {
stage: 'Customer',
lifetimeValue: 24000,
accountOwner: 'mary@contoso.com'
}
}
};
moesifMiddleware.updateUser(user, function(err) {
if (err) {
console.log('update user error');
console.log(err);
res.status(500).end();
}
res.status(201).json({ user_updated: true });
});
});
app.post('/companies(/:companyId)', function(req, res) {
// Only companyId is required.
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
// metadata can be any custom object
var company = {
companyId: req.params.companyId,
companyDomain: 'acmeinc.com', // If domain is set, Moesif will enrich your profiles with publicly available info
campaign: {
utmSource: 'google',
utmMedium: 'cpc',
utmCampaign: 'adwords',
utmTerm: 'api+tooling',
utmContent: 'landing'
},
metadata: {
orgName: 'Acme, Inc',
planName: 'Free Plan',
dealStage: 'Lead',
mrr: 24000,
demographics: {
alexaRanking: 500000,
employeeCount: 47
}
}
};
moesifMiddleware.updateCompany(company, function(err) {
if (err) {
console.log('update company error');
console.log(err);
res.status(500).end();
}
res.status(201).json({ company_updated: true });
});
});
// Parse HTTP JSON bodies
app.use(express.json());
// Parse URL-encoded params
app.use(express.urlencoded({ extended: true }));
// Host static files from the current folder
app.use(express.static(__dirname));
// Add REST API support
app.configure(express.rest());
// Configure Socket.io real-time APIs
app.configure(socketio());
// Register a nicer error handler than the default Express one
app.use(express.errorHandler());
// Add any new real-time connection to the `everybody` channel
app.on('connection', connection =>
app.channel('everybody').join(connection)
);
// Publish all events to the `everybody` channel
app.publish(data => app.channel('everybody'));
// Start the server
app.listen(3030).on('listening', () =>
console.log('Feathers server listening on localhost:3030')
);