Git Product home page Git Product logo

pixelpony's People

Contributors

mathieucaroff avatar

Stargazers

 avatar

Forkers

lwd-temp geeede

pixelpony's Issues

Explain how express-ws is used with Typescript

Copy of file server.ts:

// JS
import { readFileSync } from 'fs'
import { resolve } from 'path'

import { default as express } from 'express'
import { default as expressWs } from 'express-ws'
import { default as serveFavicon } from 'serve-favicon'

import { getOnMessage } from './getOnMessage'

// Type
import { Express } from 'express'
import { Client, Pony } from '@pixelpony/shared'
import { Registry } from './registry'

export interface ServerParam {
   client: Client
   app?: Express
}

export type Server = (param: ServerParam) => expressWs.Application

export const server: Server = (param) => {
   let { client, app } = param
   app = app || express()

   let { location } = client
   let faviconPath = resolve(location, client.favicon)
   let htmlRootPath = resolve(location, client.htmlRoot)
   let publicPath = resolve(location, client.public)

   let ews = expressWs(app)
   let wss = ews.getWss()
   // let { app } = ews

   // Favicon
   app.use(
      serveFavicon(faviconPath, {
         redirect: false,
      }),
   )

   // Known ponies
   let registry = new Registry<unknown, Pony>()

   // Ws Routes
   ews.app.ws('/websocket', (ws, req) => {
      let { onmessage } = getOnMessage({ registry, ws, wss })

      ws.onmessage = (event) => {
         let { type } = event
         if (type === 'message') {
            // ^ Maybe unecessary verification - idk ox
            onmessage(event)
         } else {
            console.log('type !== "message" -- the verification is usefull')
         }
      }
   })

   // Static Routes
   app.get('/', (req, res) => {
      res.sendFile(htmlRootPath)
   })
   let favicon = readFileSync(faviconPath)
   app.get('/favicon', (req, res) => {
      res.send(favicon)
   })
   app.use(
      express.static(publicPath, {
         redirect: false,
      }),
   )

   return ews.app
}

What does export type Server = (param: ServerParam) => expressWs.Application do?

It declares a Typescript type, used to type the server function. It appears I needed to reuse this type somewhere else in the project, which is why I wrote this declaration there. Alternatively you could directly do:

export const server = (param: ServerParam): expressWs.Application => { ... }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.