简体   繁体   中英

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.

I have built a website which runs fine locally and I want to publish it to the web using Azure.

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.

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设置

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. 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.

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)

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.

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? 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?

I try to reproduce your issue on my side, and build a simple nodejs server with sqlite3 module on Azure Web Apps. 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. So we can install the sqlite3 module on local, and deploy the node_modules folder with the application togather to Azure.
  2. As the nodejs runtime on Azure is in ia32 platform version. So we need to install with the command npm install sqlite3 --target_arch=ia32 on local.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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