Windows Azure hosts Node.js applications for storage and services

Source: Internet
Author: User
Keywords Applications Azure node.js azure

When I describe how to http://www.aliyun.com/zixun/aggregation/32995.html "> Hosting an Node.js application in Windows Azure, you might raise a question about how to use Windows Azure services, such as storage, service bus, access control, and so on. Interacting with Windows Azure services in Node.js is through the Windows Azure Node.js SDK, which is a module available in NPM. In this article I want to describe how to use Windows Azure storage (that is) and the service runtime.

Using Windows Azure Storage

Let's first look at how to use was by Node.js. We know in the previous article that you can host Node.js applications (that is, waws) and Windows Azure Cloud computing Services (that is, WACs) on the Windowsazure Web site. Theoretically, waws and some more functions are built on the role of WACS workers. So in this article I will only demonstrate hosting WACs secondary roles.

Node.js code can be used to use was when it is hosted on Waws. However, due to roles not in Waws, the use of Service runtime code mentioned in the next section cannot be used for WAWS node applications.

We can use the solution I created later. Or create a Windows Azure project for a new worker role in Visual Studio, add "Node.exe" and "index.js" and install the "Express" and "Node-sqlserver" modules to set all files as " Copy synch ".

To use Windows Azure Service, we need the Windows Azure Node.js SDK, which can be installed as a module called "Azure" via NPM. Once we download and install, we need to include them in our worker role project and set them as "Copy synch".

You can update the current Worker role project file with the "Copyall synch" tool that I mentioned in my last article. You can also find the source code for this tool here.

The source code for the Node.js Windows Azure SDK can be found on its GitHub page. It contains two sections. One of these is the CLI tool, which provides Mac and Linux Cross-platform command-line package management waws and Windows Azure virtual machines (i.e.
WAVM) for management. The other is a library for managing and using different Windows Azure services, including tables, BLOBs, queues, service buses, and service runtimes. I will not cover all but the table and service runtime information that will only demonstrate how to use this article. Here you can find the complete documentation for this SDK.

Go back to Visual Studio and open "Index.js", and let's continue with our previous article, the application for Windows Azure SQL database (i.e., W). The code should look like this.

1:var Express = require ("Express");

2:var sql = require ("Node-sqlserver");

3:

4:var connectionString = "Driver={sql Server Native Client 10.0}; Server=tcp:ac6271ya9e.database.windows.net,1433;database=synctile; uid=shaunxu@ac6271ya9e; Pwd={password}; Encrypt=yes; Connection
timeout=30; ";

5:var port = 80;

6:

7:var app = Express ();

8:

9:app.configure (function () {

10:
App.use (Express.bodyparser ());

11:});

12:

13:app.get ("/", Function (req, res) {

14:sql.open (connectionString, function (ERR, conn) {

15:if (Err) {

16:console.log (ERR);

17:res.send, Cant
Open connection. ");

18:}

19:else {

20:conn.queryraw ("Select
* FROM [Resource], function (err,
Results) {

21:if (Err) {

22:console.log (ERR);

23:res.send ("Cant retrieve records.");

24:}

25:else {

26:res.json (results);

27:}

28:});

29:}

30:});

31:});

32:

33:app.get ("/text/:key/:culture", Function (req, res) {

34:sql.open (connectionString, function (ERR, conn) {

35:if (Err) {

36:console.log (ERR);

37:res.send, Cant
Open connection. ");

38:}

39:else {

40:var key = Req.params.key;

41:var culture = req.params.culture;

42:var command = "SELECT"
* FROM [Resource] WHERE [key] = ' "+ key +
"' and [Culture] = '" + Culture + "";

43:conn.queryraw (command, function (err, results) {

44:if (Err) {

45:console.log (ERR);

46:res.send ("Cant retrieve records.");

47:}

48:else {

49:res.json (results);

50:}

51:});

52:}

53:});

54:});

55:

56:app.get ("/sproc/:key/:culture", Function (req, res) {

57:sql.open (connectionString, function (ERR, conn) {

58:if (Err) {

59:console.log (ERR);

60:res.send, Cant
Open connection. ");

61:}

62:else {

63:var key = Req.params.key;

64:var culture = req.params.culture;

65:var command = "EXEC
GetItem ' "+ key +" ', ' "+ Culture +" ";

66:conn.queryraw (command, function (err, results) {

67:if (Err) {

68:console.log (ERR);

69:res.send ("Cant retrieve records.");

70:}

71:else {

72:res.json (results);

73:}

74:});

75:}

76:});

77:});

78:

79:app.post ("/new", Function (req, res) {

80:var key =
Req.body.key;

81:var Culture =
Req.body.culture;

82:var val =
Req.body.val;

83:

84:sql.open (connectionString, function (ERR, conn) {

85:if (Err) {

86:console.log (ERR);

87:res.send, Cant
Open connection. ");

88:}

89:else {

90:var Command = INSERT
into [Resource] VALUES (' + key +
"', '" + Culture + "', N '" + val +
"')";

91:conn.queryraw (command, function (err, results) {

92:if (Err) {

93:console.log (ERR);

94:res.send ("Cant retrieve records.");

95:}

96:else {

97:res.send ("Inserted successful");

98:}

99:});

100:}

101:});

102:});

103:

104:app.listen (port);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.