コード例 #1
0
            public ExpressAppHandlerConfig(ExpressApp app, IRouterMatcher <This <IRouter> > config)
            {
                if (config == null)
                {
                    throw new ArgumentNullException(nameof(config));
                }

                _app    = app;
                _config = config;
            }
コード例 #2
0
        private static ExpressApp InitExpressApp()
        {
            // We can still use "System.Console.Write()" and "System.Console.WriteLine()" -
            // they will be translated to "console.log()" function. However, NodeJS always prints "console.log()"
            // mesages in separate lines.
            // In order to print a message without adding a new line, we can use "process.stdout":
            process.stdout.write(">> Init Express App.. ");

            try
            {
                // Create an Express app instance:
                var app = new ExpressApp();

                // Assign a request handler to the paths:
                app.Get["/sqr/:Value"] = (request, response) =>
                {
                    var args        = (RequestArgs)request.@params;
                    var val         = args.Value;
                    var responseTxt = $"sqr({val}) = {val * val}";
                    response.send.Self(responseTxt);
                };

                // Another request handler:
                app.Get["/sqrt/:Value"] = (request, response) =>
                {
                    var args        = (RequestArgs)request.@params;
                    var val         = args.Value;
                    var responseTxt = $"sqrt({val}) = {Math.Sqrt(val)}";
                    response.send.Self(responseTxt);
                };

                System.Console.WriteLine("Done!");

                return(app);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error: " + e.Message);
                return(null);
            }
        }
コード例 #3
0
        private static http.Server InitServer(ExpressApp app, int port)
        {
            process.stdout.write($">> Creating Server on port {port}.. ");

            try
            {
                // Create a Server:
                var server = http.createServer(app.AsCreateServerFn());

                // Assign Event Handlers:
                server.on(node.Literals.listening, () =>
                {
                    System.Console.WriteLine("[Server] Server is started!");
                });

                server.on(node.Literals.error, err =>
                {
                    System.Console.WriteLine("[Server] Error: " + err.message);
                });

                server.on(node.Literals.connection, socket =>
                {
                    System.Console.WriteLine($"[Server] Connection established from remote: {socket.remoteAddress}:{socket.remotePort}");
                });

                // Start listening on the specified port:
                server.listen(port);
                System.Console.WriteLine("Done!");

                return(server);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error: " + e.Message);
                return(null);
            }
        }