Skip to content

Commit e9d0438

Browse files
committed
refactor: structurize feathers app
1 parent 24f0d87 commit e9d0438

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

app.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import feathers from '@feathersjs/feathers';
2-
import '@feathersjs/transport-commons';
32
import express from '@feathersjs/express';
43
import socketio from '@feathersjs/socketio';
4+
import '@feathersjs/transport-commons';
5+
6+
import services from './services';
57

6-
import { PollService } from './PollService';
7-
import {UserService} from "./UserService";
88

99
const app = express(feathers());
1010

@@ -14,24 +14,10 @@ app.use(express.static(__dirname));
1414
app.configure(express.rest());
1515
app.configure(socketio());
1616
app.use(express.errorHandler());
17+
app.configure(services);
1718

18-
app.use('/polls', new PollService());
19-
app.use('/users', new UserService());
20-
21-
// Add any new real-time connection to the `everybody` channel
22-
app.on('connection', connection =>
23-
app.channel('everybody').join(connection)
24-
);
25-
// Publish all events to the `everybody` channel
26-
app.publish(data => app.channel('everybody'));
2719

28-
29-
app.listen(3030).on('listening', () =>
30-
console.log('Feathers server listening on localhost:3030')
31-
);
32-
33-
// For good measure let's create a message
34-
// So our API doesn't look so empty
20+
// Mock data
3521
app.service('polls').create({
3622
contents: {
3723
left: {
@@ -51,3 +37,5 @@ app.service('users').create({
5137
avatarUrl: 'https://github.com/ilyayudovin.png'
5238
});
5339

40+
export default app;
41+

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import app from './app';
2+
3+
// Add any new real-time connection to the `everybody` channel
4+
app.on('connection', connection =>
5+
app.channel('everybody').join(connection)
6+
);
7+
// Publish all events to the `everybody` channel
8+
app.publish(data => app.channel('everybody'));
9+
10+
11+
app.listen(3030).on('listening', () =>
12+
console.log('Feathers server listening on localhost:3030')
13+
);
14+

services/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Users from './users/users.service';
2+
import Polls from './polls/polls.service';
3+
4+
export default (app: Application): void => {
5+
app.configure(Users);
6+
app.configure(Polls);
7+
};
8+

services/PollService.ts renamed to services/polls/polls.class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface User {
88
avatarUrl: string;
99
}
1010

11-
export interface Poll {
11+
interface Poll {
1212
author: User;
1313
contents: {
1414
left: ImageData;
@@ -22,7 +22,7 @@ const defaultUser: User = {
2222
};
2323

2424

25-
export class PollService {
25+
export default class Polls {
2626
polls: Poll[] = [];
2727

2828
async find () {

services/polls/polls.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from '@feathersjs/express';
2+
import Polls from './polls.class';
3+
4+
export default (app: Application): void => {
5+
app.use('/polls', new Polls());
6+
};
7+

services/UserService.ts renamed to services/users/users.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface User {
44
age?: number;
55
}
66

7-
export class UserService {
7+
export default class Users {
88
users: User[] = [];
99

1010
async find (){

services/users/users.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from '@feathersjs/express';
2+
import Users from './users.class';
3+
4+
export default (app: Application): void => {
5+
app.use('/users', new Users());
6+
};
7+

0 commit comments

Comments
 (0)