const express = require("express") , expressLogging = require("express-logging") , logger = require("logops") , hostname = require('os').hostname() , browser = require('./src/service/HeadlessService') , port = 32101 , { NoSuchSessionError } = require("selenium-webdriver/lib/error") // , { puppeteerErrors } = require("puppeteer-core/lib/cjs/puppeteer/common/Errors"); logger.getContext = function getContext() { return { // hostname: hostname, // pid: process.pid, // port: port, app: 'Browser' }; } var app = express(); app.use(express.json()); app.use(expressLogging(logger)); app.use((req, res, next) => { logger.debug(Object.assign(req.body, req.query, req.params), 'Request params') next() }) app.listen(port, () => { logger.info({ app: 'Headless browser' }, 'Startup'); }); // process.stdin.resume();//so the program will not close instantly exitHandler = (options, exitCode) => { if (options.cleanup) logger.info('cleanup') if (exitCode || exitCode === 0) logger.info('exit code :', exitCode) browser.close().then(() => process.exit()); } /** * Process shutdown events handling */ //do something when app is closing process.on('exit', exitHandler.bind(null, { cleanup: true })); //catches ctrl+c event process.on('SIGINT', exitHandler.bind(null, { exit: true })); process.on('SIGTERM', exitHandler.bind(null, { cleanup: true })); // setTimeout(() => { // process.on('SIGKILL', exitHandler.bind(null, {exit:true})); // }, 2000) // catches "kill pid" (for example: nodemon restart) // process.on('SIGUSR1', exitHandler.bind(null, {exit:true})); // process.on('SIGUSR2', exitHandler.bind(null, {exit:true})); //catches uncaught exceptions // process.on('uncaughtException', exitHandler.bind(null, {exit:true})); /** * request handler */ app.post('/', async (req, res, next) => { browser.get(req.body).then(html => { if (html) res.send(html) else throw new Error('Emtpy html') }).catch(e => { if (e instanceof NoSuchSessionError) { logger.error({ reason: e.message, url: req.url }, 'Browser session expired') } else { logger.error({ reason: e.message, url: req.url }, 'Browser error occured') } res.status(500).send({ code: 500, message: e.message }) }) })