M
M
MaksN92022-03-13 18:10:47
Node.js
MaksN9, 2022-03-13 18:10:47

How to disable express in Node.js?

You need to change the code so that it does not have express.
Please tell me how can I do this

const express = require("express");
const bcrypt = require ('bcrypt');
const fs = require('fs');
const app = express();   
const urlencodedParser = express.urlencoded({extended: false});

function createHash(userPassword) {
  const salt = bcrypt.genSaltSync(10);
  const hash = bcrypt.hashSync(userPassword, salt);
  return {
      hash,
      salt,
  }
}

function checkUser(userName, userPassword) {
    const data = fs.readFileSync('users.txt', 'utf-8');

    const users = []
    data.split(';').forEach(item => {
        if (!item.length) return

        const [userName, hash] = item.split(' ')
        users.push({
            userName,
            hash
        })
    })

    const currentUser = users.find(item => item.userName === userName)

    if (!currentUser) {
        const { hash, salt } = createHash(userPassword)
        const newData = `${data}${userName} ${hash} ${salt};`

        fs.writeFileSync('users.txt', newData, 'utf-8');
        return true
    }

    return bcrypt.compareSync(Buffer.from(userPassword), currentUser.hash)
}

app.use(express.static(__dirname));
  
app.get("/", function (request, response) {
    response.sendFile(__dirname + "/index.html");
});

app.post("/", urlencodedParser, function (request, response) {
    const { userName, userPassword } = request?.body || {}
    if (!userName ||
        userName === '' ||
        !userPassword ||
        userPassword == '' ||
        /\s/g.test(userName)
    ) {
        return response.sendStatus(400);
    }

    const isLoggedIn = checkUser(userName, userPassword)
    if (!isLoggedIn) response.sendStatus(400)
    response.sendStatus(200)
});

app.listen(3000, () => console.log('Server work'));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lssssssssssl, 2022-03-13
@lsssssssssl

const express = require("express");
Delete this line, and then all the lines that your ide will highlight in red.
And there will be no express in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question