From 629faae8ab64a82a7328ccc0170edacd29bab2c3 Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Mon, 28 Mar 2022 13:03:02 +0100 Subject: [PATCH] added serverjs --- server.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 server.js diff --git a/server.js b/server.js new file mode 100644 index 0000000..7c4711d --- /dev/null +++ b/server.js @@ -0,0 +1,84 @@ +const next = require("next"); +const { createServer } = require("http"); +const appInsights = require("applicationinsights"); + +const initAppInsights = (instrumentationKey) => { + if (!instrumentationKey) { + return false; + } + + appInsights + .setup(instrumentationKey) + .setAutoCollectConsole(true, true) + .setSendLiveMetrics(true) + .start(); + + return true; +}; + +const startServer = async (config) => { + const serverOptions = { + dev: config.env === "development", + dir: ".", + quiet: false, + }; + + const app = next(serverOptions); + const handleNextRequests = app.getRequestHandler(); + + const srv = createServer((req, res) => { + if (config.useAppInsights) { + appInsights.defaultClient.trackNodeHttpRequest({ + request: req, + response: res, + }); + } + + handleNextRequests(req, res); + }); + + await new Promise((resolve, reject) => { + // This code catches EADDRINUSE error if the port is already in use + srv.on("error", reject); + srv.on("listening", () => resolve()); + srv.listen(config.port, config.hostname); + }); + + // It's up to caller to run `app.prepare()`, so it can notify that the server + // is listening before starting any intensive operations. + return app; +}; + +const startTime = Date.now(); + +const serverConfig = { + hostname: "localhost", + port: process.env.PORT || 3000, + env: process.env.APP_ENV || process.env.NODE_ENV || "production", + useAppInsights: initAppInsights( + process.env.NEXT_PUBLIC_APPINSIGHTS_INSTRUMENTATIONKEY + ), +}; + +startServer(serverConfig) + .then(async (app) => { + console.log( + `started server on host ${serverConfig.hostname}, port ${serverConfig.port}, env ${serverConfig.env}` + ); + + if (serverConfig.useAppInsights) { + const duration = Date.now() - startTime; + + appInsights.defaultClient.trackMetric({ + name: "server startup time", + value: duration, + }); + } + + await app.prepare(); + }) + .catch((err) => { + console.error(err); + + process.exit(1); + });