简体   繁体   English

将带有生成密码的变量从后端传递到前端

[英]Pass variable with generated password from backend into frontend

I am trying to pass my variable with generated password into my front-end side.我正在尝试将带有生成密码的变量传递到我的前端。

I think that my main problem is that I am generating password inside我认为我的主要问题是我在里面生成密码

route.post路由.post

this is my piece of fileUpload.route.ts这是我的一块fileUpload.route.ts

router.post('/upload-file', upload.array('file', 6), (req:any, res:any, next:any) => {
//...
genPass();
}

Inside genPass() is里面genPass()

let password = generator.generate({
  length: 10,
  numbers: true
});

I tried to do我试着做

module.exports = router, genPass;

and

router.get('/getpassword', fileController.fileGeneratedPassword);

Then inside my Controller然后在我的控制器里面

const password = require('../routes/fileUpload.route');

class FileController {
  public async fileGeneratedPassword(req: Request, res: Response): Promise<void> {
        console.log('pass: ' + JSON.stringify(password));
        res.json(password);
    }
}

But console.log is always empty.但是console.log总是空的。

How should I pass this variable?我应该如何传递这个变量?

I think that I must generate password inside router.post because when I upload in my frontend file then I want to generate password at the same time to "encrypt file".我认为我必须在router.post 中生成密码,因为当我在前端文件中上传时,我想同时生成密码来“加密文件”。

I want to display generated password in my Angular frontend我想在我的 Angular 前端显示生成的密码

module.exports returns a single value. module.exports 返回单个值。 you can return an object if you like but I'd separate them into 2 files如果您愿意,您可以返回一个对象,但我会将它们分成 2 个文件

generator发电机

function genPass() {
    return generator.generate({
        length: 10,
        numbers: true
    });
}

module.exports = genPass

controller控制器

const genPass = require('generator');

class FileController {
  public async fileGeneratedPassword(req: Request, res: Response): Promise<void> {
        const password = genPass()
        console.log('pass: ' + JSON.stringify(password));
        res.json(password);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM