-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (51 loc) · 1.86 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
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
// A message service that allows us to create new messages and return all
// existing messages.
class MessageService {
constructor() {
this.messages = [];
}
async find () {
// Just return all the messages
return this.messages;
}
async create (data) {
// The new message is the data merged with a unique identifier using
// the messages length since it changes whenever we add a message
const message = {
id: this.messages.length,
text: data.text
}
// Add the new message to the array of messages
this.messages.push(message);
return message;
}
}
// Creates an ExpressJS compatiable Feathers app
const app = express(feathers());
// Parse HTTP JSON bodies
app.use(express.json());
// Parse URL-encoded parameters
app.use(express.urlencoded({ extended: true }));
// Host static files from the current folder/directory
app.use(express.static(__dirname));
// Add REST API support
app.configure(express.rest());
// Configure Socket.io real-time APIs
app.configure(socketio());
// Register an in-memory message service
app.use('/messages', new MessageService());
// Register a better error handler than the default Express handler
app.use(express.errorHandler());
// Add any new real-time connections to the 'everybody' channel
app.on('connection', connection => app.channel('everbody').join(connection));
// Puyblish all events to the 'everbody' channel
app.publish(data => app.channel('everybody'));
// Start the server
app.listen(3030).on('listening', () => console.log('Feathers server listening on port 3030'));
// Create a sample, default message to populate the initial state of the service
app.service('messages').create({
text: 'Hell world, from the new server!'
});