Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pyrite-framework/pyrite-server-emitter
pyrite-server emitter plugin
https://github.com/pyrite-framework/pyrite-server-emitter
pyrite-server socket-io
Last synced: about 2 months ago
JSON representation
pyrite-server emitter plugin
- Host: GitHub
- URL: https://github.com/pyrite-framework/pyrite-server-emitter
- Owner: pyrite-framework
- License: mit
- Created: 2017-10-12T21:38:01.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-18T22:57:10.000Z (over 7 years ago)
- Last Synced: 2024-10-12T00:27:06.456Z (4 months ago)
- Topics: pyrite-server, socket-io
- Language: TypeScript
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyrite-server-emitter
## Install
- Decorators feature has to be enabled.
```
npm install pyrite-server
npm install pyrite-server-emitter
```## Example
### main.js
```typescript
import { PyriteServer } from "pyrite-server";
import { EmitterPlugin } from "pyrite-server-emitter";const server = new PyriteServer({
port: 8000,
routes: "/routes",
plugins: [new EmitterPlugin()]
});server.listen(() => {
console.log("Server running!");
});
```### /routes folder:
### users.js
```typescript
import {
Route, Get, Post, Put, Delete, Exception, Body, Params, Query
} from "pyrite-server";import {
Emits, Emit, Broadcast
} from "pyrite-server-emitter";const users = [];
let index = 0;@Route("/users")
export class Users {
@Get("/")
getUsers(@Query("name") name) {
const result = users.filter((user) => !name || user.name === name);
return result;
}@Post("/")
@Broadcast
createUser(@Body user, @Emit emit) {
user.id = index++;users.push(user);
emit(user);
return user;
}@Put("/:id", Number)
@Broadcast
updateUser(@Body user, @Emit emit) {
const foundUser = users.find((localUser) => localUser.id === user.id);
if (!user) throw Exception(404, "not_found");Object.assign(foundUser, user);
emit(user);
return user;
}@Delete("/:id", Number)
@Emits
removeUser(@Params("id") id, @Emit emit) {
const indexUser = users.findIndex((user) => user.id === id);
if (indexUser === -1) throw Exception(404, "not_found");users.splice(indexUser, 1);
emit(id);
return true;
}
}
```