http#createServer JavaScript Examples

The following examples show how to use http#createServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: server.js    From HinataMd with GNU General Public License v3.0 6 votes vote down vote up
function connect(conn, PORT) {
    let app = global.app = express()
    console.log(app)
    let server = global.server = createServer(app)
    // app.use(express.static(path.join(__dirname, 'views')))
    let _qr = 'invalid'

    conn.ev.on('connection.update', function appQR({ qr }) {
        if (qr) _qr = qr
    })

    app.use(async (req, res) => {
        res.setHeader('content-type', 'image/png')
        res.end(await toBuffer(_qr))
    })

    // let io = new Socket(server)
    // io.on('connection', socket => {
    //     let { unpipeEmit } = pipeEmit(conn, socket, 'conn-')
    //     socket.on('disconnect', unpipeEmit)
    // })

    server.listen(PORT, () => {
        console.log('App listened on port', PORT)
        if (opts['keepalive']) keepAlive()
    })
}
Example #2
Source File: socket-io-api.js    From SquadJS with Boost Software License 1.0 6 votes vote down vote up
constructor(server, options, connectors) {
    super(server, options, connectors);

    this.httpServer = createServer();

    this.io = new Server(this.httpServer, {
      cors: {
        origin: 'http://localhost:3000',
        methods: ['GET', 'POST']
      }
    });

    this.io.use((socket, next) => {
      if (socket.handshake.auth && socket.handshake.auth.token === this.options.securityToken) {
        next();
      } else {
        next(new Error('Invalid token.'));
      }
    });

    this.io.on('connection', (socket) => {
      this.verbose(1, 'New Connection Made.');
      this.bindListeners(socket, this.server);
      this.bindListeners(socket, this.server.rcon, 'rcon.');
      // Events to broadcast
      for (const eventToBroadcast of eventsToBroadcast) {
        this.server.on(eventToBroadcast, (...args) => {
          socket.emit(eventToBroadcast, ...args);
        });
      }
    });
  }
Example #3
Source File: index.js    From playwright-test with MIT License 6 votes vote down vote up
/**
 * Get a free port
 *
 * @param {number} port
 * @param {string} host
 * @returns {Promise<number>}
 */
function getPort(port = 3000, host = '127.0.0.1') {
  const server = createServer()

  return new Promise((resolve, reject) => {
    server.on('error', (err) => {
      // @ts-ignore
      if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
        server.listen(0, host)
      } else {
        reject(err)
      }
    })
    server.on('listening', () => {
      // @ts-ignore
      const { port } = server.address()

      server.close(() => resolve(port))
    })
    server.listen(port, host)
  })
}
Example #4
Source File: http-bridge.js    From loa-details with GNU General Public License v3.0 6 votes vote down vote up
export function setupBridge(appSettings) {
  httpServer = createServer((req, res) => {
    const isHostValid = checkHost(req.headers.host);
    if (!isHostValid) {
      log.info("Request from invalid host: " + req.headers.host);
      res.writeHead(403, { "Content-Type": "text/html" });
      return res.end("Forbidden");
    }

    if (req.method === "POST") {
      // Handle data
      let body = [];

      req.on("data", (chunk) => {
        body.push(chunk);
      });

      req.on("end", function () {
        const parsedBody = Buffer.concat(body).toString();
        httpServerEventEmitter.emit("packet", parsedBody);

        res.writeHead(200, { "Content-Type": "text/html" });
        res.end("Ok!");
      });
    }
  });

  httpServer.listen(0, "localhost", () => {
    log.info(`Server listening on port ${httpServer.address().port}`);
    validHosts.push(
      `localhost:${httpServer.address().port}`,
      `127.0.0.1:${httpServer.address().port}`
    );
    httpServerEventEmitter.emit("listen");
    spawnPacketCapturer(appSettings, httpServer.address().port);
  });
}
Example #5
Source File: www.js    From TDD-NodeJS-Containers with Apache License 2.0 5 votes vote down vote up
server = createServer(app)
Example #6
Source File: xoa.js    From smart-node-tpl with MIT License 5 votes vote down vote up
// 创建服务器实例,并执行相应任务
  createServer() {
    const server = createServer((req, res) => {
      // 应用中间件
      this.middleware.forEach((fn) => fn(req, res))
    })
    return server
  }
Example #7
Source File: xoa.js    From smart-node-tpl with MIT License 5 votes vote down vote up
// 服务器监听
  listen(port = 3000, cb) {
    this.createServer().listen(port, cb)
  }