Skip to content

Commit

Permalink
docs: Add SocketIO example
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 3, 2019
1 parent c19a532 commit 931753e
Show file tree
Hide file tree
Showing 25 changed files with 11,701 additions and 101 deletions.
119 changes: 58 additions & 61 deletions docs/getting-started.md
Expand Up @@ -121,45 +121,43 @@ a new `Server` class that extends [`ServerLoader`](/docs/server-loader.md).

```typescript
import {ServerLoader, ServerSettings, GlobalAcceptMimesMiddleware} from "@tsed/common";
import Path = require("path");
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const compress = require('compression');
const methodOverride = require('method-override');
const rootDir = __dirname;

@ServerSettings({
rootDir: Path.resolve(__dirname), // optional. By default it's equal to process.cwd()
acceptMimes: ["application/json"]
rootDir,
acceptMimes: ["application/json"]
})
export class Server extends ServerLoader {

/**
* This method let you configure the express middleware required by your application to works.
* @returns {Server}
*/
public $onMountingMiddlewares(): void|Promise<any> {

const cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
compress = require('compression'),
methodOverride = require('method-override');

this
.use(GlobalAcceptMimesMiddleware)
.use(cookieParser())
.use(compress({}))
.use(methodOverride())
.use(bodyParser.json())
.use(bodyParser.urlencoded({
extended: true
}));

return null;
}

public $onReady(){
console.log('Server started...');
}
/**
* This method let you configure the express middleware required by your application to works.
* @returns {Server}
*/
public $onMountingMiddlewares(): void|Promise<any> {
this
.use(GlobalAcceptMimesMiddleware)
.use(cookieParser())
.use(compress({}))
.use(methodOverride())
.use(bodyParser.json())
.use(bodyParser.urlencoded({
extended: true
}));

return null;
}

public $onReady(){
console.log('Server started...');
}

public $onServerInitError(err){
console.error(err);
}
public $onServerInitError(err){
console.error(err);
}
}

new Server().start();
Expand Down Expand Up @@ -197,43 +195,42 @@ So the controller's url will be `http://host/rest/calendars`.

```typescript
import {
Controller, Get, Render, Post,
Authenticated, Required, BodyParams,
Delete
Controller, Get, Render, Post,
Authenticated, Required, BodyParams,
Delete
} from "@tsed/common";

export interface Calendar{
id: string;
name: string;
id: string;
name: string;
}

@Controller("/calendars")
export class CalendarCtrl {

@Get("/")
@Render("calendars/index")
async renderCalendars(): Promise<Array<Calendar>> {
return [{id: '1', name: "test"}];
}

@Post("/")
@Authenticated()
async post(
@Required() @BodyParams("calendar") calendar: Calendar
): Promise<Calendar> {
return new Promise<Calendar>((resolve: Function, reject: Function) => {
calendar.id = "1";
resolve(calendar);
});
}
@Get("/")
@Render("calendars/index")
async renderCalendars(): Promise<Array<Calendar>> {
return [{id: '1', name: "test"}];
}

@Post("/")
@Authenticated()
async post(
@Required() @BodyParams("calendar") calendar: Calendar
): Promise<Calendar> {
calendar.id = "1";

@Delete("/")
@Authenticated()
async deleteItem(
@BodyParams("calendar.id") @Required() id: string
): Promise<Calendar> {
return {id, name: "calendar"};
}
return Promise.resolve(calendar);
}

@Delete("/")
@Authenticated()
async deleteItem(
@BodyParams("calendar.id") @Required() id: string
): Promise<Calendar> {
return {id, name: "calendar"};
}
}
```

Expand Down
39 changes: 24 additions & 15 deletions docs/tutorials/README.md
Expand Up @@ -2,28 +2,37 @@

## Starters

* [Quick start project](https://github.com/Romakita/ts-express-decorators/tree/production/integration/getting-started)
* [Node Api Starter](https://github.com/scopsy/node-typescript-starter) by [Scopsy](https://github.com/scopsy)
- [Quick start project](https://github.com/Romakita/ts-express-decorators/tree/production/integration/getting-started)
- [Node Api Starter](https://github.com/scopsy/node-typescript-starter) by [Scopsy](https://github.com/scopsy)

## Demo

Some examples are available along these links :

**Projects examples for v4.0.0 and more**
**Projects example for v5.x.x**

* [Basics usages](https://github.com/Romakita/ts-express-decorators/tree/production/integration/getting-started)
* [Https](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-https)
* [Authentication with Passport.js](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-passport)
* [Mongoose & Swagger](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-mongoose)
* [TypeORM](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-typeorm)
- [Basic usages](https://github.com/Romakita/ts-express-decorators/tree/production/integration/getting-started)
- [Https](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-https)
- [A SquareGame with SocketIO](https://github.com/Romakita/ts-express-decorators/tree/production/integration/socketio)
- [Authentication with Passport.js](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-passport)
- [Mongoose & Swagger](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-mongoose)
- [TypeORM](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-typeorm)

**Projects examples for v4.x.x**

- [Basic usages](https://github.com/Romakita/ts-express-decorators/tree/production/integration/getting-started)
- [Https](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-https)
- [Authentication with Passport.js](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-passport)
- [Mongoose & Swagger](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-mongoose)
- [TypeORM](https://github.com/Romakita/example-ts-express-decorator/tree/4.0.0/example-typeorm)

**Projects examples for v2.0.0 and more**

* [Basics usages](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-basic)
* [Https](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-https)
* [Service declaration](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-services)
* [Authentication with Passport.js](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-passport)
* [Mongoose and deserialization](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-mongoose)
* [Swagger UI](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-swagger)
* [Testing](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-testing)
- [Basic usages](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-basic)
- [Https](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-https)
- [Service declaration](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-services)
- [Authentication with Passport.js](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-passport)
- [Mongoose and deserialization](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-mongoose)
- [Swagger UI](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-swagger)
- [Testing](https://github.com/Romakita/example-ts-express-decorator/tree/2.0.0/example-testing)

4 changes: 4 additions & 0 deletions docs/tutorials/socket-io.md
Expand Up @@ -11,6 +11,10 @@ meta:

Socket.io enable real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.

## Example

A complete example is available [here](https://github.com/Romakita/ts-express-decorators/tree/production/integration/socketio).

## Installation

Before using the Socket.io, we need to install the [Socket.io](https://www.npmjs.com/package/socket.io) module.
Expand Down
29 changes: 14 additions & 15 deletions docs/tutorials/templating.md
Expand Up @@ -8,11 +8,17 @@ This decorator will use the response return by the method and will use the view
## Installation
This example use EJS as engine rendering. To use other engine, see the documentation of the concerned project.
```typescript
import Path = require("path");
const rootDir = Path.resolve(__dirname);
import {ServerSettings, ServerLoader} from "@tsed/common"
const cons = require("consolidate");
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const compress = require('compression');
const methodOverride = require('method-override');
const rootDir = __dirname;

@ServerSettings({
rootDir,
viewsDir: `${rootDir}/views`,
mount: {
'/rest': `${rootDir}/controllers/**/**.js`
},
Expand All @@ -22,14 +28,12 @@ const rootDir = Path.resolve(__dirname);
})
class Server extends ServerLoader {

async $onMountingMiddlewares() {

const cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
compress = require('compression'),
methodOverride = require('method-override'),
session = require('express-session');
$onInit(){
this.set("views", this.settings.get('viewsDir')); // le repertoire des vues
this.engine("ejs", cons.ejs);
}

async $onMountingMiddlewares() {
this.use(ServerLoader.AcceptMime("application/json"))
.use(bodyParser.json())
.use(bodyParser.urlencoded({
Expand All @@ -38,11 +42,6 @@ class Server extends ServerLoader {
.use(cookieParser())
.use(compress({}))
.use(methodOverride());

// EJS Engine Setting
this.engine('.html', require('ejs').__express)
.set('views', './views')
.set('view engine', 'html');
}
}
```
Expand All @@ -56,7 +55,7 @@ class Server extends ServerLoader {
export class EventCtrl {

@Get("/:id")
@Render("eventCard")
@Render("eventCard.ejs")
public get(request: Express.Request, response: Express.Response): any {
return {startDate: new Date(), name: "MyEvent"};
}
Expand Down
1 change: 0 additions & 1 deletion integration/getting-started/package.json
Expand Up @@ -6,7 +6,6 @@
"scripts": {
"clean": "rimraf '{src,test}/**/*.{js,js.map}'",
"build": "npm run tsc",
"postinstall": "npm run build",
"test": "npm run test:lint && npm run test:unit",
"test:unit": "cross-env NODE_ENV=test nyc --reporter=html --reporter=text mocha --recursive",
"test:lint": "tslint --project tsconfig.json",
Expand Down
16 changes: 7 additions & 9 deletions integration/getting-started/src/Server.ts
Expand Up @@ -2,8 +2,14 @@ import {GlobalAcceptMimesMiddleware, ServerLoader, ServerSettings} from "@tsed/c
import "@tsed/swagger";
import {$log} from "ts-log-debug";

const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const compress = require("compression");
const methodOverride = require("method-override");
const rootDir = __dirname;

@ServerSettings({
rootDir: __dirname,
rootDir,
acceptMimes: ["application/json"],
logger: {
debug: false,
Expand All @@ -18,19 +24,11 @@ import {$log} from "ts-log-debug";
}
})
export class Server extends ServerLoader {

/**
* This method let you configure the middleware required by your application to works.
* @returns {Server}
*/
$onMountingMiddlewares(): void | Promise<any> {

const cookieParser = require("cookie-parser"),
bodyParser = require("body-parser"),
compress = require("compression"),
methodOverride = require("method-override");


this
.use(GlobalAcceptMimesMiddleware)
.use(cookieParser())
Expand Down
58 changes: 58 additions & 0 deletions integration/socketio/.gitignore
@@ -0,0 +1,58 @@
### Node template
.DS_Store
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
.npmrc
*.log

# Typings
typings/

# Typescript
src/**/*.js
src/**/*.js.map
test/**/*.js
test/**/*.js.map

# Test
/coverage
/stdout
/stderr
/es6/
/lib/
/dts/
/.tmp
/.nyc_output/

# IDE
.vscode
.idea

# Project
public

0 comments on commit 931753e

Please sign in to comment.