-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
63 lines (46 loc) · 1.25 KB
/
index.ts
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
import { feathers } from '@feathersjs/feathers'
import { bodyParser, errorHandler, koa, rest, serveStatic } from '@feathersjs/koa'
import socketio from '@feathersjs/socketio'
interface Message {
id?: number
text: string
}
class MessageService {
messages: Message[] = []
async find() {
return this.messages
}
async create(data: Pick<Message, 'text'>) {
const message: Message = {
id: this.messages.length,
text: data.text
}
this.messages.push(message)
return message
}
async remove(data: Message) {
if (data.id && (data.id < 0 || data.id >= this.messages.length)) {
this.messages.splice(data.id, 1);
return;
}
this.messages = [];
}
}
type ServiceTypes = {
messages: MessageService
}
const app = koa<ServiceTypes>(feathers())
app.use(serveStatic('.'))
app.use(errorHandler())
app.use(bodyParser())
app.configure(rest())
app.configure(socketio())
app.use('messages', new MessageService())
app.on('connection', (connection) => app.channel('everybody').join(connection))
app.publish((_data) => app.channel('everybody'))
app
.listen(3030)
.then(() => console.log('Feathers server listening on localhost:3030'))
app.service('messages').create({
text: 'Hello world from the server'
})