app.ready().then(() => { console.log('app(%s) ready', app.config.baseDir); const server = http.createServer(app.callback()); // emit server event just like egg-cluster does // https://github.com/eggjs/egg-cluster/blob/master/lib/app_worker.js#L52 app.emit('server', server); server.listen(7001, () => { console.log('Server started at 7001'); }); });
// Initializes the server and mounts it in the generated build directory. http.createServer((req, res) => { // If we are accessing the root, it should be redirected to the default language, // We shouldn't get a 404 page. if (req.url === '/') { req.url = `/${mainLocale}` } mount(req, res) }).listen(port, () => { console.log(`\x1B[32mServer running at http://localhost:${port}/${mainLocale}/\x1B[39m`) })
http.createServer((request, response) => { response.end(); })
test("should not be able to listen if the server is already listening", async () => { // arrange process.env.CUBEJS_ENABLE_TLS = "false"; let cubeServer = new CubeServer(); // act try { await cubeServer.listen(); await cubeServer.listen(); } catch (err) { // assert expect(err.message).toBe("CubeServer is already listening"); expect(http.createServer).toHaveBeenCalledTimes(1); expect(http.__mockServer.listen).toHaveBeenCalledTimes(1); expect(cubeServer.server).not.toBe(null); } });
test( "given that CUBEJS_ENABLE_TLS is not true, " + "should create an http server that listens to the PORT", async () => { // arrange const server = new CubeServer(); // act await server.listen(); // assert expect(http.createServer).toHaveBeenCalledTimes(1); expect(http.__mockServer.listen).toHaveBeenCalledTimes(1); expect(http.__mockServer.listen.mock.calls[0][0]).toBe(4000); } );
/** * Initialize reporter * @param {MetricRegistry} registry * @memberof PrometheusReporter */ init(registry) { super.init(registry); this.server = http.createServer(); this.server.on("request", this.handler.bind(this)); this.server.listen(this.opts.port, err => { if (err) { /* istanbul ignore next */ return this.registry.broker.fatal(new MoleculerError("Prometheus metric reporter listening error: " + err.message)); } this.logger.info(`Prometheus metric reporter listening on http://0.0.0.0:${this.opts.port}${this.opts.path} address.`); }); this.defaultLabels = isFunction(this.opts.defaultLabels) ? this.opts.defaultLabels.call(this, registry) : this.opts.defaultLabels; }
http.createServer(function (req, res) { var url = URL.parse(req.url); res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(fs.readFileSync(__dirname + url.pathname)); })
test( "given that CUBEJS_ENABLE_TLS is true, " + "should create an http server listening to PORT to redirect to https", async () => { // arrange process.env.CUBEJS_ENABLE_TLS = "true"; const server = new CubeServer(); // act await server.listen(); // assert expect(http.createServer).toHaveBeenCalledTimes(1); expect(http.createServer.mock.calls[0][0].toString()).toMatchSnapshot(); expect(server.redirector).toBe(http.__mockServer); expect(http.__mockServer.listen).toHaveBeenCalledTimes(1); expect(http.__mockServer.listen.mock.calls[0][0]).toBe(4000); } );
function startProxyServer() { const port = 3123; const requestHandler = (request, response) => { response.writeHead(200, { "Content-Type": "application/json" }); response.end( JSON.stringify({ path: request.url }) ); }; const server = http.createServer(requestHandler); server.listen(port, err => { if (err) { return console.log("something bad happened", err); } }); return server; }
test('upload - bad response', function (t) { const dataDirectory = path.resolve( __dirname, 'fixtures', 'only-folder', '10000.clinic-doctor' ) const server = http.createServer(function (req, res) { res.destroy() }) server.listen(0, '127.0.0.1', function () { const uploadUrl = `http://localhost:${server.address().port}` tarAndUpload(dataDirectory, uploadUrl, null, {}, function (err) { t.strictEqual(err.message, 'socket hang up') server.close(() => t.end()) }) }) })
test('Before all', function (t) { server = http.createServer(() => {}) websocket.createServer({ server }, conn => { conn.on('data', token => { cliToken = token.toString('utf8') if (simulateTimeout) { conn.write('timeout\n') } else if (simulateNoToken) { conn.write('\n') } else { conn.write('jwtToken\n') } conn.end() }) }) server.listen(0, function () { t.plan(1) t.ok(server) }) })
test("given an option object, should pass the options object to the created server instance", async () => { // arrange process.env.CUBEJS_ENABLE_TLS = "true"; let options = { key: true, cert: true }; let server = new CubeServer(); // act await server.listen(options); // assert expect(https.createServer.mock.calls[0][0]).toBe(options); // arrange process.env.CUBEJS_ENABLE_TLS = "false"; options = { key: true, cert: true }; server = new CubeServer(); // act await server.listen(options); // assert expect(http.createServer.mock.calls[1][0]).toBe(options); });
test('upload - bad status code', function (t) { const dataDirectory = path.resolve( __dirname, 'fixtures', 'only-folder', '10000.clinic-doctor' ) const server = http.createServer(function (req, res) { res.statusCode = 500 res.end() }) server.listen(0, '127.0.0.1', function () { const uploadUrl = `http://localhost:${server.address().port}` tarAndUpload(dataDirectory, uploadUrl, null, {}, function (err) { t.strictDeepEqual(err, new Error('Bad status code: 500')) server.close(() => t.end()) }) }) })
test('upload - bad body encoding', function (t) { const dataDirectory = path.resolve( __dirname, 'fixtures', 'only-folder', '10000.clinic-doctor' ) const server = http.createServer(function (req, res) { res.end('not JSON') }) server.listen(0, '127.0.0.1', function () { const uploadUrl = `http://localhost:${server.address().port}` tarAndUpload(dataDirectory, uploadUrl, null, {}, function (err) { t.strictEqual(err.name, 'SyntaxError') server.close(() => t.end()) }) }) })
function startProxyServer() { const port = 3123; const requestHandler = (request, response) => { response.writeHead(200, { "Content-Type": "application/json" }); response.end( JSON.stringify({ path: request.url }) ); }; const server = http.createServer(requestHandler); server.listen(port, err => { if (err) { return console.log("something bad happened", err); } }); return server; }