Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.use(routes());
const httpServer = app.listen(port, host, backlog, () => {
const { address, port } = server.address();
console.log(`The Snack web server is listening on http://${address}:${port}`);
});
httpServer.timeout = timeout;
// In development, it's common to stop or restart the server so we immediately end and close all
// sockets when stopping the server instead of waiting for the requests to finish. In production,
// we allow the requests a grace period to complete before ending and closing the sockets.
const gracePeriod = process.env.NODE_ENV === 'development' ? 0 : httpServer.timeout;
const server = stoppable(httpServer, gracePeriod);
// Listen to HTTP server error events and handle shutting down the server gracefully
let exitSignal = null;
let httpServerError = null;
server.on('error', error => {
httpServerError = error;
console.error(`There was an error with the HTTP server:`, error);
console.error(`The HTTP server is shutting down and draining existing connections`);
server.stop();
});
server.on('close', () => {
console.log(`The HTTP server has drained all connections and is scheduling its exit`);
console.log(`The HTTP server process is exiting...`);
// Let other "close" event handlers run before exiting
app.use(bodyParser());
app.use(routes());
const httpServer = app.listen(port, host, backlog, () => {
const { address, port } = server.address() as AddressInfo;
console.log(`The Snack web server is listening on http://${address}:${port}`);
});
httpServer.timeout = timeout;
// In development, it's common to stop or restart the server so we immediately end and close all
// sockets when stopping the server instead of waiting for the requests to finish. In production,
// we allow the requests a grace period to complete before ending and closing the sockets.
const gracePeriod = process.env.NODE_ENV === 'development' ? 0 : httpServer.timeout;
const server = stoppable(httpServer, gracePeriod);
// Listen to HTTP server error events and handle shutting down the server gracefully
let exitSignal: ShutdownSignal | null = null;
let httpServerError: Error | null = null;
server.on('error', error => {
httpServerError = error;
console.error(`There was an error with the HTTP server:`, error);
console.error(`The HTTP server is shutting down and draining existing connections`);
server.stop();
});
server.on('close', () => {
console.log(`The HTTP server has drained all connections and is scheduling its exit`);
console.log(`The HTTP server process is exiting...`);
// Let other "close" event handlers run before exiting
/* ===
Manage the Server: start and stops
=== */
import express from 'express';
import fs from 'fs';
import http from 'http';
import socket from 'socket.io';
import stoppable from 'stoppable';
import userHome from 'user-home';
import Watcher from './Watcher';
import SocketManager from './SocketManager';
const app = express();
const server = stoppable(http.Server(app));
const PUBLIC_PATH = `${userHome}/Carbon`;
const HOST = 'http://localhost:';
let PORT;
const io = socket(server);
const watcher = new Watcher();
const socketManager = new SocketManager();
// Create path if it does not exist yet
if (!fs.existsSync(PUBLIC_PATH)) {
fs.mkdir(PUBLIC_PATH);
}
// Start the server
const startServer = port => {
PORT = port || 3333;
if (config.http.express.trustProxy) {
// see: https://stackoverflow.com/questions/23413401/what-does-trust-proxy-actually-do-in-express-js-and-do-i-need-to-use-it
expressApp.enable('trust proxy');
}
const { service, logger, presenter } = app({
auth: config.auth,
http: config.http,
logger: config.logger,
repo: config.repo,
translator: config.translator,
});
expressApp.all('*', presenter);
const server = stoppable(createServer(expressApp));
/* @credits: https://github.com/banzaicloud/node-service-tools */
handleUnexpectedEvents({ server, service, logger });
server.listen(config.http.express.port, () => {
logger.info(
`Listening on ${config.http.express.host}:${config.http.express.port}`
);
});
checkNamedPipe(ipcPath);
// Remove `port` so that `path` is honored
delete this.serverOptions.port;
}
this._protocol = serverOptions ? serverOptions.protocol ?? 'http' : 'http';
if (this._protocol === 'https') {
this.server = https.createServer(
this.serverOptions as https.ServerOptions,
this.requestListener,
);
} else {
this.server = http.createServer(this.requestListener);
}
// Set up graceful stop for http server
if (typeof this.serverOptions.gracePeriodForClose === 'number') {
this._stoppable = stoppable(
this.server,
this.serverOptions.gracePeriodForClose,
);
}
}
listen(port?: number, host?: string) {
const errorHandler = (error: any) => {
console.error("Internal service error:", error)
server.close(() => process.exit(1))
}
const handler = service.handler(errorHandler)
const server = stoppable(http.createServer(handler), gracefulCloseTimeout)
return new Promise(resolve => {
server.listen(port, host, () => resolve(server))
})
}
}