function startServer() { const server = new grpc.Server(); server.addService(BookServiceService, new ServerImpl()); server.bind("127.0.0.1:50051", grpc.ServerCredentials.createInsecure()); server.start(); log("Server started, listening: 127.0.0.1:50051"); }
// Global error handler // Note : calling next(err) will call this error handler if no other handler id defined.You can handle custom error over here const errorHandler = (err, req, res, next) => { log(err.toString()) if (err.name === 'AppError') return res.status(err.status).json({ error: err.message, stack: process.env.APP_ENVIROMENT === 'dev' ? err.stack : undefined }) if (err.name === 'UnauthorizedError') return res.status(httpStatus.UNAUTHORIZED).json({ error: err.message }) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: process.env.APP_ENVIROMENT === 'dev' ? err.message : 'Oops !! Something went wrong' }) }
getBook(call, callback) { const book = new book_pb_1.Book(); book.setTitle("DefaultBook"); book.setAuthor("DefaultAuthor"); log(`[getBook] Done: ${JSON.stringify(book.toObject())}`); callback(null, book); }
init () { if (this._protocol === 'tcp') throw Error('Useless call to init() in TCP mode, use connect() instead') if (!this._port) throw Error('Port is mandatory in UDP') udpClient.call(this, this._verbose ? debug : log('netcat:client:udp')) return this }
listen () { debug('listen called') if (this._protocol === 'tcp') { tcpServer.call(this, this._verbose ? debug : log('netcat:server:tcp')) this.server.listen(this._unixSocket || { host: this._address, port: this._port }) } else { // udp if (!this._port) throw Error('listen(): setting a port is mandatory.') udpServer.call(this, this._verbose ? debug : log('netcat:server:udp')) } return this /* chainable method */ }
function ajax(userOpts, options, callback) { var reqAjax = userOpts.ajax || {}; var reqOpts = extend(clone(ajaxOpts), reqAjax, options); log(reqOpts.method + ' ' + reqOpts.url); return utils.ajax(reqOpts, callback); }
clientB.on('connect', () => { log('client B connected'); conn += 1; if(conn === 2) cb(); });
getBooksViaAuthor(call: grpc.ServerWriteableStream<GetBookViaAuthorRequest>) { log(`[getBooksViaAuthor] Request: ${JSON.stringify(call.request.toObject())}`); for (let i = 1; i <= 10; i++) { const reply = new Book(); reply.setTitle(`Book${i}`); reply.setAuthor(call.request.getAuthor()); reply.setIsbn(i); log(`[getBooksViaAuthor] Write: ${JSON.stringify(reply.toObject())}`); call.write(reply); } log("[getBooksViaAuthor] Done."); call.end(); }
eventSource.addEventListener('message', ev => { try { const data: { type: string; data: string[] } = JSON.parse( (ev as MessageEvent).data ); log(`Message from stream - ${data.type}`); if (data.type === 'error') { showNotif('Server error', 'See console for details'); log(data.data.join('\n')); } } catch (ex) { log(ex); } });
mongoose.connection.on('connected', () => { log('MongoDB Connection Established') })
connect (cb) { if (this._protocol !== 'tcp') { throw Error('Useless call to connect() in UDP mode, use init() instead') } tcpClient.call(this, this._verbose ? debug : log('netcat:client:tcp'), cb) return this }
const preload = async (loadable, path) => { log(`[start] Pre-fetching bundle for ${path}`) // fetch Page component(bundle). const Component = await loadable.load() // fetch initialProps of Page. const initialProps = await getInitialPropsFromComponent(Component, path) log(`[end] Pre-fetching bundle for ${path} initialProps=`, initialProps) return initialProps }
getBook(call: grpc.ServerUnaryCall<GetBookRequest>, callback: grpc.sendUnaryData<Book>) { const book = new Book(); book.setTitle("DefaultBook"); book.setAuthor("DefaultAuthor"); log(`[getBook] Done: ${JSON.stringify(book.toObject())}`); callback(null, book); }