code node js

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
11
Indexable
Never
const http = require("http");
const fs = require("fs");
const url = require("url");
const oracledb = require("oracledb");
const logger = require("./logger");
const dbConfig = require("./dbconfig2.js");
const libPath = require("./libpath");
const hostname = "localhost";
const port = 3010;

const server = http.createServer((req, res) => {
    const route = url.parse(req.url, true).pathname;
    logger.log(
        "info",
        `requete http: ${req.url} \n - route ${route} méthode: ${req.method}`
    );
    changermdp(req, res);
});



async function changermdp(req, res) {
    const route = url.parse(req.url, true).pathname;
    logger.log("info", `parametre route: ${route}`);
    const objectParams = url.parse(req.url, true).query;
    let p_utilisateur = objectParams.username;
    let p_mdp = objectParams.password;
    let connection;
    let message = "";
    logger.log("info", `paramètre route : ${route}`);
    logger.log("info", `utilisateur : ${p_utilisateur}, mot de passe : ${p_mdp}`);
    if(!p_utilisateur || !p_mdp) {
        console.log("il y a des valeurs NULL");
    }
    try {
        connection = await oracledb.getConnection(dbConfig);
        logger.log("info", "Connexion réussi à la base de données Oracle");

        //appel de la fonction
        let query = "UPDATE emp SET job=:mdp WHERE ename=:utilisateur";
        let result = await connection.execute(query, {
            utilisateur: p_utilisateur,
            mdp: p_mdp,
        });

        if (result.rowsAffected == 0) {
            message= "utilisateur nexiste pas";
        } else {
            message = "le mot de passe a ete modifier";
        }
        res.statusCode = 200;
        res.setHeader("Content-Type", "text/html");
        res.end(message);
    } catch (err) {

    } finally {
        if (connection) {
            try {
                await connection.close();
            } catch (err) {

            }
        }
    }
}

//MAIN
if (libPath && fs.existsSync(libPath)) {
    oracledb.initOracleClient({ libDir: libPath });
  }
  server.listen(port, hostname, () => {
    logger.log(
      "info",
      `Le serveur roule à l'URL suivant: http://${hostname}:${port}/`
    );
  });