let express = require("express"); let app = express(); let server = require("http").Server(app); let morgan = require("morgan"); let io = require("socket.io")(server); let https = require("https"); let fs = require("fs"); server.listen(process.env.PORT || 8080); let indexRoute = require("./routes/index"); let notFoundRoute = require("./routes/notFound"); let errorRoute = require("./routes/error"); app.use(morgan("dev")) .use(express.static("public")) .set("view engine", "pug") .use("/", indexRoute) .use(notFoundRoute) .use(errorRoute); io.on("connection", (socket) => { socket.emit("connected"); console.log("New connection !"); socket.on("checkMail", (data) => { let key = fs.readFileSync("key", "utf-8"); let request = https.request({ hostname: "haveibeenpwned.com", port: 443, path: "/api/v3/breachedaccount/" + data.email, method: "GET", headers: { Accept: 'application/json', "user-agent": "InternetSecurity", "hibp-api-key": key } }, res => { let sendData = false; res.setEncoding("utf-8"); res.on("data", d => { socket.emit("resultMail", d); sendData = true; }); res.on("close", () => { if (!sendData) socket.emit("resultMail", []); }); }); request.on("error", err => { console.error(err); socket.emit("resultMail", null); }); request.end(); }) }); module.exports = server;