简体   繁体   English

如何在Angular控制器中使用Browserized npm软件包?

[英]How to use Browserified npm package in Angular controller?

I've spent hours looking for a way to get this (or anything) working with Browserify, but could not find any tutorial or example that proved useful for my problem. 我已经花了数小时来寻找一种使它(或任何东西)与Browserify一起工作的方法,但是找不到任何证明对我的问题有用的教程或示例。 Nearly all of them only show how to blundle your code, but none of them show how to actually access the code itself after it's been browserified. 几乎所有这些都仅显示如何使代码模糊,但没有一个显示出在浏览器代码之后如何实际访问代码本身。 Maybe it's just something so straightforward that no one bothered to mention it, so my apologies if this is actually a silly question. 也许这只是一件很简单的事情,没人理会提及它,所以我很抱歉,如果这实际上是一个愚蠢的问题。

So, I have this application which implements username and password validation. 因此,我有一个实现用户名和密码验证的应用程序。 I use the 'validator' npm package in combination with an OWASP recommended password complexity package to validate the input on the server side in node. 我将“ validator” npm软件包与OWASP建议的密码复杂性软件包结合使用,以验证节点中服务器端的输入。 From what I understand from the Browserify project, I should be able to browserify my module which require these packages, and load them on the client side too. 从我对Browserify项目的了解中,我应该能够对需要这些程序包的模块进行浏览器化,并将其也加载到客户端。

My customValidator.js module now looks like this: 我的customValidator.js模块现在看起来像这样:

var validator = require('validator')
var owasp = require('owasp-password-strength-test')

module.exports = {
  validator: validator,
  owasp: owasp,
  containsUsername: function(password, username) {
    return (new RegExp(username, 'i')).test(password)
  }
}

I want to use these functions on the client side too, preferably within AngularJS as I use Angular to manipulate the input fields based on the current (possibly invalid) input the user has provided. 我也想在客户端使用这些功能,最好在AngularJS中使用,因为我使用Angular根据用户提供的当前(可能无效)输入来操纵输入字段。

Basically what I would like is symmetric input validation on both the client side as the server side by having access to the same module on both sides, which has been browserified for the client side. 基本上,我想要的是在客户端和服务器端都进行对称输入验证,方法是在客户端和服务器端都可以访问相同的模块。

I have ran the following command: 我已经运行了以下命令:

browserify --standalone customValidator.js > clientSideValidator.js

And then included the clientSideValidor.js script within my jade template 然后在我的Jade模板中包含clientSideValidor.js脚本

  script(src='clientSideValidator.js')

I have tried to access the functions in both Angular and a separate script within jade itself, but every function always returns undefined. 我试图在Angular和jade本身内的单独脚本中访问函数,但是每个函数总是返回未定义的。 How can I run something like 'validator.isAlphaNumeric($scope.username)' in my client side code? 如何在客户端代码中运行类似“ validator.isAlphaNumeric($ scope.username)”的内容?

You need to give browserify a name to export everything to. 您需要给browserify一个名称,以将所有内容导出到该名称。

Try running this and replace 'myModuleName' with what you would like to name it 尝试运行此命令并将“ myModuleName”替换为您想要的名称

$ browserify customValidator.js --standalone myModuleName > clientSideValidator.js

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

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