How to use the @loopback/http-server.HttpServer function in @loopback/http-server

To help you get started, we’ve selected a few @loopback/http-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github raymondfeng / loopback4-example-websocket / src / application.ts View on Github external
constructor(options: ApplicationConfig = {}) {
    super(options);

    /**
     * Create an Express app to serve the home page
     */
    const expressApp = express();
    const root = path.resolve(__dirname, '../../public');
    expressApp.use('/', express.static(root));

    // Create an http server backed by the Express app
    this.httpServer = new HttpServer(expressApp, options.websocket);

    // Create ws server from the http server
    const wsServer = new WebSocketServer(this.httpServer);
    this.bind('servers.websocket.server1').to(wsServer);
    wsServer.use((socket, next) => {
      console.log('Global middleware - socket:', socket.id);
      next();
    });
    // Add a route
    const ns = wsServer.route(WebSocketController, /^\/chats\/\d+$/);
    ns.use((socket, next) => {
      console.log(
        'Middleware for namespace %s - socket: %s',
        socket.nsp.name,
        socket.id,
      );
github strongloop / loopback4-example-shopping / packages / recommender / src / recommendation-rest.ts View on Github external
export function createRecommendationServer(
  port = 3001,
  host: string | undefined = undefined,
) {
  const app = express();

  app.get('/:userId', (req: express.Request, res: express.Response) => {
    let userId = (req.params as ParamsDictionary).userId || 'user001';
    if (!(userId in recommendations)) {
      userId = 'user001';
    }
    res.send(recommendations[userId] || []);
  });

  return new HttpServer(app, {port, host});
}

@loopback/http-server

A wrapper for creating HTTP/HTTPS servers

MIT
Latest version published 1 month ago

Package Health Score

92 / 100
Full package analysis

Popular @loopback/http-server functions

Similar packages