是否有可能将“乏味"的浏览器浏览到浏览器中?模块,以便nodejs程序可以在浏览器中运行? [英] Is it possible to browserify the "tedious" module so that the nodejs program can be run in the browser?

查看:132
本文介绍了是否有可能将“乏味"的浏览器浏览到浏览器中?模块,以便nodejs程序可以在浏览器中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node.js的初学者,目前正在构建一个Node.js程序,该程序使用乏味"模块(请参见下面的代码)访问和查询Microsoft Azure SQL数据库并将数据放入html中.网页.我想在浏览器中运行此代码,因此我使用browserify将模块捆绑在一起.但是,当此代码在Google Chrome浏览器中运行时,返回以下错误:未定义require.有解决办法吗?甚至可以在Chrome中使用乏味的模块吗?如果不可能,是否需要在Node.js应用程序和网页之间使用中间服务器?

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! :)

推荐答案

否.该模块只能在Node中使用.

No. This module can only be used in Node.

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

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.

即使可以在浏览器中使用此模块,这也是一个糟糕的主意.您将允许您网站上的任何人直接连接到SQL Server并运行SQL查询. 这只会很糟糕地结束.

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.

这篇关于是否有可能将“乏味"的浏览器浏览到浏览器中?模块,以便nodejs程序可以在浏览器中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆