【问题标题】:Is it possible to browserify the "tedious" module so that the nodejs program can be run in the browser?是否可以将“繁琐”的模块浏览化,以便 nodejs 程序可以在浏览器中运行?
【发布时间】:2017-01-08 08:43:51
【问题描述】:

我是 Node.js 的初学者,我目前正在构建一个 Node.js 程序,该程序使用“繁琐”的模块(参见下面的代码)访问和查询 Microsoft Azure SQL 数据库,并将数据放入 html网页。我想在浏览器中运行这段代码,所以我使用 browserify 将模块捆绑在一起。但是,当这段代码在谷歌浏览器中运行时,会返回以下错误:require is not defined。有解决办法吗?甚至可以在 Chrome 中使用繁琐的模块吗?如果不可能,是否需要在 Node.js 应用程序和网页之间使用中间服务器?

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'hackmatch',  
    password: 'hackvalley123!',  
    server: 'hackmatch.database.windows.net',  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  
var connection = new Connection(config);  
connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
    console.log("Connected");  
});  

var Request = require('tedious').Request;  
var TYPES = require('tedious').TYPES;

提前感谢您的帮助! :)

【问题讨论】:

  • require is not defined 听起来更像是 Browserify(或您如何使用它)的问题,而不是您使用的任何单个模块的问题。如果您尝试使用其他模块而不是 tedious 模块,您是否仍然会收到这些错误?
  • 我刚刚用 express 测试了 require ,它仍然没有工作(同样的错误)。这可能与我使用 browserify 的方式有关
  • 您是否在捆绑 JS 文件之外调用 require

标签: node.js azure-sql-database browserify


【解决方案1】:

没有。该模块只能在Node中使用。

繁琐的依赖于 node.js net 模块来建立与数据库服务器的连接。该模块在浏览器上没有等效模块,因为网页无法建立任意网络连接。

即使可以在浏览器中使用这个模块,这也是一个糟糕的主意。您将允许您网站上的任何人直接连接到您的 SQL 服务器并运行 SQL 查询。 This can only end badly.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2012-01-17
    • 2012-06-03
    相关资源
    最近更新 更多