Is it possible to browserify the “tedious” module so that the nodejs program can be run in the browser?

只谈情不闲聊 提交于 2019-12-02 05:13:50

问题


I'm a beginner to Node.js and I'm currently building a Node.js program that accesses and queries a Microsoft Azure SQL database with the "tedious" module (see code below) and puts the data onto a html webpage. I want to run this code in a browser so I used browserify to bundle the modules together. However, when this code is run in Google Chrome, the following error is returned: require is not defined. Is there a fix? Is it even possible to use the tedious module in Chrome? If it isn't possible, do I need to use an intermediate server between the Node.js application and the webpage?

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;

Thanks in advance for your help! :)


回答1:


No. This module can only be used in Node.

tedious depends on the node.js net module to make a connection to the database server. This module has no equivalent on the browser, as web pages cannot make arbitrary network connections.

Even if it were possible to use this module in the browser, it'd be a terrible idea. You'd be allowing anyone on your web site to connect directly to your SQL server and run SQL queries. This can only end badly.



来源:https://stackoverflow.com/questions/41528190/is-it-possible-to-browserify-the-tedious-module-so-that-the-nodejs-program-can

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!