简体   繁体   English

在Azure上运行Node.js服务器和数据库

[英]Running a Node.js server and database on Azure

As a preface I am new to web development and have never published a site before. 作为序言,我是Web开发的新手,以前从未发布过网站。

I have built a website which runs fine locally and I want to publish it to the web using Azure. 我已经建立了一个在本地运行良好的网站,我想使用Azure将其发布到Web。

The site uses a node.js server which I wrote myself (No express) which is connected to an SQLite3 database using the sqlite3 node module. 该站点使用的是我自己编写的node.js服务器(无快递),该服务器使用sqlite3节点模块连接到SQLite3数据库。

All I want to do is publish this site, I've tried using Azure to do so by using Azure command line tools to create a web app from the Git repo I have for the site. 我要做的就是发布此网站,我已经尝试使用Azure通过使用Azure命令行工具从该网站的Git存储库中创建Web应用程序来进行发布。

我拥有的Azure设置

I have a package.json file pointing to the server.js file which is the backend for the site, as well as serving files in the site it also returns data from an SQLite3 database I also have in the site folder. 我有一个指向server.js文件的package.json文件,该文件是网站的后端,并且在网站中提供文件,它还从我在网站文件夹中也有的SQLite3数据库返回数据。 I also have the web.config file from this: https://github.com/projectkudu/kudu/wiki/Using-a-custom-web.config-for-Node-apps with the path to my server changed to match mine. 我也有以下的web.config文件: https : //github.com/projectkudu/kudu/wiki/Using-a-custom-web.config-for-Node-apps ,我的服务器路径已更改为与我的服务器匹配。

When I try to visit the site all I get is a blank screen, the application log gives this error: Error: SQLITE_CANTOPEN: unable to open database file at Error (native) 当我尝试访问该站点时,得到的只是一个空白屏幕,应用程序日志中出现以下错误: Error: SQLITE_CANTOPEN: unable to open database file at Error (native)

So I'm guessing this means it has a problem having the DB built into the site in this way, if I comment out the database stuff it loads the site (minus the db content) just fine. 因此,我猜测这意味着将数据库以这种方式内置到站点中会出现问题,如果我注释掉数据库中的内容,它将可以很好地加载站点(减去数据库内容)。 When I try to test running the server in the azure console I get a "Bad Request" error, running on my own machine works fine. 当我尝试在azure控制台中测试运行服务器时,出现“错误请求”错误,在我自己的计算机上运行正常。

My question is basically, how should I go about this goal of getting the site up given the challenges I've got? 我的问题基本上是,考虑到我面临的挑战,我应该如何实现建立站点的目标? Is having an integrated db file completely the wrong approach or can I make it work? 拥有集成的db文件是完全错误的方法还是可以使它工作? I've played around creating an azure DB but I cannot work out how to get the data from my db file into it. 我玩过创建一个蔚蓝数据库的过程,但是我无法弄清楚如何从我的数据库文件中获取数据。 Are azure virtual machines the way to go, the advice I read was they're for more computationally intensive projects I'm only hosting a site? Azure虚拟机是否可行,我读到的建议是,它们适用于我只托管一个站点的计算密集型项目吗?

I try to reproduce your issue on my side, and build a simple nodejs server with sqlite3 module on Azure Web Apps. 我尝试重现您的问题,并在Azure Web Apps上使用sqlite3模块构建一个简单的nodejs服务器。 But it works fine on my side, here are my test steps, you can try to follow my steps to fix your issue. 但这对我来说很好用,这是我的测试步骤,您可以尝试按照我的步骤解决问题。

  1. As to install sqlite3 requiring node-pre-gyp which is kind of Native Modules not supported via deployment task on Azure Web Apps. 至于要安装sqlite3需要node-pre-gyp ,这是Azure Web Apps上的部署任务不支持的本Native Modules So we can install the sqlite3 module on local, and deploy the node_modules folder with the application togather to Azure. 因此,我们可以在本地安装sqlite3模块,并与应用程序一起将node_modules文件夹部署到Azure。
  2. As the nodejs runtime on Azure is in ia32 platform version. 由于Azure上的nodejs运行时处于ia32平台版本。 So we need to install with the command npm install sqlite3 --target_arch=ia32 on local. 因此,我们需要在本地使用命令npm install sqlite3 --target_arch=ia32进行npm install sqlite3 --target_arch=ia32
  3. Here is the code on my test: 这是我测试中的代码:

     var http = require("http"); var server = http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write("<!DOCTYPE html>"); response.write("<html>"); response.write("<head>"); response.write("<title>Hello World Page</title>"); response.write("</head>"); response.write("<body>"); var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database('test.db'); var data = []; db.serialize(function() { db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)"); var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); for (var i = 0; i < 10; i++) { stmt.run("Ipsum " + i); } stmt.finalize(); db.each("SELECT * FROM lorem", function(err, row) { data.push(row); }, function() { response.write(JSON.stringify(data)); response.write("</body>"); response.write("</html>"); response.end(); }); }); db.close(); }); server.listen(process.env.PORT || 1337); 

Any further concern, please feel free to let me know. 如有任何其他疑问,请随时告诉我。

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

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