`
bk_lin
  • 浏览: 321969 次
社区版块
存档分类
最新评论

Node.js 体验 — 当 Node.js 邂逅 Windows Azure

 
阅读更多

在本系列的第一篇文章中,我介绍了如何在Windows上运行Node.js。在第二篇文章中,我示范了如何在Node.js 中使用SQL Server。那也是我学习Node.js 的步骤,首先使它在windows上工作,然后再在SQL Server上。但是回到我开始的目标上,我需要为Wang Tao的worktile实现同步服务,并打算在Windows Azure上建一个原型。所以前两篇文章是准备工作。本文我将描述如何在Windows Azure上托管一个Node.js网站。

通过Git将Node.js部署在Windows Azure网站上

Windows Azure网站 (以下简称 WAWS)是微软六月份Meet Windows Azure 活动中推出的新服务。它提供了低成本、内置的模版,并易于部署到云端。获取更多WAWS信息,请参看我之前的文章:

我认为WAWS是我们部署Node.js网站最简易的方式。

- IIS和IISNode已在WAWS环境中安装配置好,所以我们部署时不用安装任何东西。

- WAWS 支持多种部署方法,包括TFS、FTP以及Git。对Node.js来说,FTP和Git较简单快捷。

- WAWS提供三种不同的扩展模式,他们都是免费的、共享的、保留的。若我们只需一个具备数据库的网站,那这样的花费比Windows Azure云服务(以下简称 WACS)要低的。

- WAWS支持Windows Azure SQL数据库(以下简称WASD)和MySQL。

我们来看下,用Node.js在WAWS上建立一个网站是多么的简单、容易。首先,我们需要创建一个新的WAWS。由于本文后面我要示范如何从Node.js中用WASD,我们也将创建一个WASD。前往windows azure developer portal,从NEW button中选择COMPUTERàWEB SITEà CREATE WITH DATABASE。

网站一旦创建,我们前往它的控制面板,点击右侧的Set up Git publishing链接。

片刻之后,Windows Azure会完成Git部署配置。如果这是你第一次在Windows Azure上配置Git或FTP 部署,你需要点击控制面板上的Reset deployment credentials链接来为部署提供用户名和密码。

接下来,我们把Git资源库从Windows Azure复制到本地磁盘。如果你机器上没装Git,点击这里下载。安装完后,我们需要从开始菜单àGit文件夹中打开Git Bash。在命令提示符窗口中,前往我们想复制的文件夹中。例如,在我的例子中,我将用 “D:\Research\nodejs\apps\nodejssample”,所以我需要前往““D:\Research\nodejs\apps”并保证“nodejssample”文件夹不存在,因为在复制时,Git会创建这一文件夹。

回到开发者入口部署页面,复制Git的URL,然后执行如下的Git 复制命令。当需要密码时,给出前一步我们设定的值。

现在资源库已从WAWS上复制到本地磁盘中。然后,我们会用另外一个叫做GitHub for Windows的GUI工具提交推送我们的更改。

——GitHub for Windows是一个运行在Windows 上的GUI工具,能够轻易控制GitHub上的资源库。我们也能用这个工具控制从WAWS上复制下来的资源库。获取此工具,点击 这里

打开GitHub for Windows,打开我们刚在文件浏览器中复制的资源库,并将此文件夹拖入GitHub for Windows。

但我们在GitHub窗口点击这个资源库时,我们需要输入在开发者端口指定的证书。至此,本地文件夹尚未改变。

由于WAWS通过IIS和IISNode托管Node.js应用程序,它会开启叫做“server.js”的JavaScript文件。所以我们必须创建一个叫做“server.js”的源文件,这是我们网站的入口。建立一个web server并让其监听来自“process.env.PORT”的端口。

——“process.env.PORT” 代表它会从环境变量中检索名为“PORT”的端口号。由于WAWS在Windows Azure路由器和防火墙后面托管所有的网站,此环境变量代表我们的WAWS正在监听的正确内部端口。

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

2:

3: http.createServer(function (req, res) {

4:
res.writeHead(200, {"Content-Type": "text/plain"});

5:
res.end("Hello Node.js and Windows Azure Website!\n");

6: }).listen(process.env.port);

7:

8: console.log("Server started.");

一旦我们保存文件,返回GitHub窗口,我们会发现它检测到了变动,然后我们可以将其提交。

然后点击窗口顶部的“publish”按钮。它会向WAWS远程资源库发布我们的变动,然后WAWS开始部署。

现在回到windows azurwe开发者端口,这个网站下的部署页面会有一个新的部署,试一下吧。

Windows Azure网站中的NPM Modules

用NPM模块,以及用我们的Node.js网站单独部署模块也很简单。我们添加的所有模块都位于 “node_modules”子文件夹下,所以我们不需要多余的工作。

例如,我在我的网站中通过NPM命令安装了“express”模块。它把所有需要的文件下载到“node_modules”子文件夹下。然后我会修改代码来使用“express”。

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

2: var app = express();

3:

4: app.get("/", function(req, res)
{

5:
res.send("Hello Node.js, Express and Windows Azure Web
Site.");

6: });

7:

8: app.get("/Echo/:value",
function(req, res) {

9:
var value = req.params.value;

10:
res.json({

11:
"Value" : value,

12:
"Time" : new Date()

13:
});

14: });

15:

16: console.log("Web application
opened.");

17: app.listen(process.env.PORT);

然后前往GitHub,提交并与远程资源库同步。

然后在windows azure中我们会找到新部署。

如果我们刷新网站主页,我们会发现新的内容。我们也可以测试我们在变动中添加的新函数。

用Windows Azure SQL 数据库工作

我们继续用之前文章中提到的SQL图表和数据,执行下列我开始时创建的SQL数据库脚本。

1: /****** Object: Table [dbo].[Resource] Script Date: 9/4/2012 3:47:14 PM ******/

2: SET ANSI_NULLS ON

3: GO

4: SET QUOTED_IDENTIFIER ON

5: GO

6: CREATE TABLE [dbo].[Resource](

7:
[Key] [varchar](256) NOT NULL,

8:
[Culture] [varchar](8) NOT NULL,

9:
[Value] [nvarchar](4000) NOT NULL,

10:
CONSTRAINT [PK_Resource] PRIMARY KEY CLUSTERED

11: (

12:
[Key] ASC,

13:
[Culture] ASC

14: )WITH (STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF)

15: )

16:

17: GO

18: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeAbout_Message', N'en-US', N'Your
app description page.')

19: GO

20: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeAbout_Message', N'zh-CN', N'你的关于页面。')

21: GO

22: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeContact_Message', N'en-US', N'Your
contact page.')

23: GO

24: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeContact_Message', N'zh-CN', N'你的联系信息页面。')

25: GO

26: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeIndex_Message', N'en-US', N'Modify
this template to jump-start your ASP.NET MVC application.')

27: GO

28: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Controller_HomeIndex_Message', N'zh-CN', N'修改次模板页,快速开始您的ASP.NET MVC应用程序。')

29: GO

30: INSERT [dbo].[Resource] ([Key], [Culture],
[Value]) VALUES (N'Model_AccountModels_LoginModel_Password_Display', N'en-US',
N'Password')

31: GO

32: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_LoginModel_Password_Display',
N'zh-CN', N'密码')

33: GO

34: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_LoginModel_Password_Required', N'en-US', N'Please input
{0}.')

35: GO

36: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_LoginModel_Password_Required',
N'zh-CN', N'请输入{0}。')

37: GO

38: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_LoginModel_RememberMe_Display', N'en-US', N'Remember
me?')

39: GO

40: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_LoginModel_RememberMe_Display', N'zh-CN', N'记住登录状态?')

41: GO

42: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_LoginModel_UserName_Display',
N'en-US', N'User Name')

43: GO

44: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_LoginModel_UserName_Display',
N'zh-CN', N'用户名')

45: GO

46: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_LoginModel_UserName_Required',
N'en-US', N'Please input the {0}.')

47: GO

48: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_LoginModel_UserName_Required', N'zh-CN', N'请输入{0}。')

49: GO

50: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_RegisterModel_ConfirmPassword_Compare', N'en-US', N'The
password and confirmation password do not match.')

51: GO

52: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_RegisterModel_ConfirmPassword_Compare', N'zh-CN', N'两次输入的密码不一致。')

53: GO

54: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_RegisterModel_ConfirmPassword_Display', N'en-US',
N'Confirm password')

55: GO

56: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_RegisterModel_ConfirmPassword_Display', N'zh-CN', N'再次输入密码')

57: GO

58: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'Model_AccountModels_RegisterModel_Password_StringLength',
N'en-US', N'The {0} must be at least {2} characters long.')

59: GO

60: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES
(N'Model_AccountModels_RegisterModel_Password_StringLength', N'zh-CN', N'{0}长度不足。')

61: GO

62: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_ExtenalAccount', N'en-US',
N'Use another service to log in.')

63: GO

64: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_ExtenalAccount', N'zh-CN', N'使用其他服务登录。')

65: GO

66: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_LocalAccount', N'en-US', N'Use
a local account to log in.')

67: GO

68: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_LocalAccount', N'zh-CN', N'使用本地帐户登录。')

69: GO

70: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_RegisterIfNoAccount', N'en-US',
N'{0} if you don''t have an account.')

71: GO

72: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountLogin_RegisterIfNoAccount', N'zh-CN',
N'如果没有账户,请{0}。')

73: GO

74: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountRegister_Message', N'en-US', N'Create
a new account.')

75: GO

76: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_AccountRegister_Message', N'zh-CN', N'创建一个新用户。')

77: GO

78: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_ExternalLoginsListPartial_MessageInfo',
N'en-US', N'There are no external authentication services configured. See <a
href="http://go.microsoft.com/fwlink/?LinkId=252166">this
article</a> for details on setting up this ASP.NET application to support
logging in via external services.')

79: GO

80: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_ExternalLoginsListPartial_MessageInfo',
N'zh-CN', N'没有配置任何第三方认证服务。关于如何在ASP.NET应用程序中配置和使用第三方认证服务,请访问此<a
href="http://go.microsoft.com/fwlink/?LinkId=252166">文章</a>。')

81: GO

82: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_ExternalLoginsListPartial_SocialLoginList',
N'en-US', N'Log in using another service')

83: GO

84: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_ExternalLoginsListPartial_SocialLoginList',
N'zh-CN', N'用其它认证服务登录')

85: GO

86: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeAbout_Title', N'en-US', N'About')

87: GO

88: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeAbout_Title', N'zh-CN', N'关于')

89: GO

90: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeContact_Title', N'en-US', N'Contact')

91: GO

92: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeContact_Title', N'zh-CN', N'联系信息')

93: GO

94: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeIndex_Title', N'en-US', N'Home Page')

95: GO

96: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomeIndex_Title', N'zh-CN', N'首页')

97: GO

98: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Featured', N'en-US', N'To learn
more about ASP.NET MVC visit <a href="http://asp.net/mvc"
title="ASP.NET MVC Website">http://asp.net/mvc</a>. The page
features <mark>videos, tutorials, and samples</mark> to help you
get the most from ASP.NET MVC. If you have any questions about ASP.NET MVC
visit <a href="http://forums.asp.net/1146.aspx/1?MVC"
title="ASP.NET MVC Forum">our forums</a>.')

99: GO

100: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Featured', N'zh-CN', N'要了解更多关于ASP.NET MVC的信息请访问<a
href="http://asp.net/mvc" title="ASP.NET MVC网站">http://asp.net/mvc</a>。该页面提供<mark>视频,教程和例子</mark>,以帮助你获得对全面的ASP.NET MVC资讯。如果您有任何关于ASP.NET
MVC的问题,请访问我们的<a
href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC论坛">论坛</a>。')

101: GO

102: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest', N'en-US', N'We suggest
the following:')

103: GO

104: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest', N'zh-CN', N'我们建议:')

105: GO

106: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_1_Title', N'en-US',
N'Getting Started')

107: GO

108: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_1_Title', N'zh-CN', N'入门')

109: GO

110: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_1_Val', N'en-US', N'ASP.NET
MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and that gives you full control over
markup for enjoyable, agile development. ASP.NET MVC includes many features
that enable fast, TDD-friendly development for creating sophisticated
applications that use the latest web standards. <a
href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn
more...</a>')

111: GO

112: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_1_Val', N'zh-CN', N'ASP.NET
MVC为您提供了一个强大的、基于模式的方式来构建动态网站,使一个干净的关注点分离,让您愉快,敏捷开发的完全控制权的标记。
ASP.NET MVC包含了许多功能,使快速创建复杂的应用程序,使用最新的Web标准,TDD友好的开发。<a
href="http://go.microsoft.com/fwlink/?LinkId=245151">了解更多…</a>')

113: GO

114: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_2_Title', N'en-US', N'Add
NuGet packages and jump-start your coding')

115: GO

116: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_2_Title', N'zh-CN', N'添加NuGet软件包,快速开始编码')

117: GO

118: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_2_Val', N'en-US', N'NuGet
makes it easy to install and update free libraries and tools. <a
href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn
more...</a>')

119: GO

120: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_2_Val', N'zh-CN', N'NuGet让安装和更新免费的代码库和工具变得异常容易。<a
href="http://go.microsoft.com/fwlink/?LinkId=245153">了解更多…</a>')

121: GO

122: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_3_Title', N'en-US', N'Find
Web Hosting')

123: GO

124: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_3_Title', N'zh-CN', N'寻找虚拟主机')

125: GO

126: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_3_Val', N'en-US', N'You can
easily find a web hosting company that offers the right mix of features and
price for your applications. <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn
more...</a>')

127: GO

128: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_HomIndex_Suggest_3_Val', N'zh-CN', N'您可以很容易地找到一个Web托管公司,提供为您的应用程序的功能和价格的最佳组合。<a
href="http://go.microsoft.com/fwlink/?LinkId=245157">了解更多…</a>')

129: GO

130: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_Layout_LogoHere', N'en-US', N'your logo
here')

131: GO

132: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_Layout_LogoHere', N'zh-CN', N'在这儿放置图标')

133: GO

134: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_Layout_Title', N'en-US', N'My ASP.NET MVC
Application')

135: GO

136: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_Layout_Title', N'zh-CN', N'我的ASP.NET
MVC应用程序')

137: GO

138: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Login', N'en-US', N'Log in')

139: GO

140: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Login', N'zh-CN', N'登录')

141: GO

142: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Logoff', N'en-US', N'Log off')

143: GO

144: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Logoff', N'zh-CN', N'登出')

145: GO

146: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Register', N'en-US',
N'Register')

147: GO

148: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_LoginPartial_Register', N'zh-CN', N'注册')

149: GO

150: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_About', N'en-US', N'About')

151: GO

152: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_About', N'zh-CN', N'关于')

153: GO

154: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_Contact', N'en-US', N'Contact')

155: GO

156: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_Contact', N'zh-CN', N'联系信息')

157: GO

158: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_Home', N'en-US', N'Home')

159: GO

160: INSERT [dbo].[Resource] ([Key],
[Culture], [Value]) VALUES (N'View_PageName_Home', N'zh-CN', N'首页')

161: GO

然后,我们需要在本地资源库中添加node-sqlserver模块。就像我之前文章提到的, node-sqlserver与SQL Server和WASD协同工作。但如果我们用NPM安装,就会产生问题。不知你是否记得,我在文章中指出,当NPM安装了node-sqlserver时,它会通过Python产生一些C++代码。输出进制取决于本地计算机和Python是x86还是x64。如果我们的本地计算机是x86的,那就没问题。但如果我们本地计算机是X64的,那么node-sqlserver模块就会在WAWS上不可用,因为在WAWS上,所有的网站是托管在x86 WOW模式下的IIS上的。

一种解决方案是把我们开发的计算机换成x86,这不一定令人满意,因为对其他Windows Azure服务开发,如WASD和WACS而言,x64是首选。

我们也可以用一台x86计算机来下载、编译一个node-sqlserver 的x86版本,然后把它复制到我们的工作机上。

我们也可以从微软下载 x86版本的node-sqlserver,名字是 “Microsoft Driver for Node.JS for SQL Server Preview“,点击 这里下载。根据提示,我们可以安装,并复制“node_modules”文件夹下的“node-sqlserver”文件夹到我们的资源库。

现在,我们就可以用类似之前文章中的源代码来操作WASD了,只需改变连接字符串。代码如下:

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

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

3:

4: var connectionString = "Driver={SQL
Server Native Client 10.0};Server=tcp:{YOUR SERVER
NAME}.database.windows.net,1433;Database=nodejssample;Uid={YOUR LOGIN}@{YOUR
SERVER NAME};Pwd={YOUR PASSWORD};Encrypt=yes;Connection Timeout=30;";

5: var port = process.env.PORT

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(500, "Cannot open
connection.");

18:
}

19:
else {

20: conn.queryRaw("SELECT * FROM
[Resource]", function(err, results) {

21: if(err) {

22: console.log(err);

23: res.send(500, "Cannot
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(500, "Cannot 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(500, "Cannot
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(500, "Cannot 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(500, "Cannot
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(500, "Cannot 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(500, "Cannot
retrieve records.");

95:
}

96: else {

97: res.send(200,
"Inserted Successful");

98: }

99: });

100:
}

101:
});

102: });

103:

104: app.listen(port);

——你可以在开发者端口的WASD页面上找到连接字符串。在Node.js中,我们需要用ODBC连接字符串,并在提交和同步前更改密码。

在GitHub窗口中保存文件,并提交、同步。我们的网站会自动部署。

如果我们略微改变C#控制台应用程序,我们就可以测试托管在WAWS上Node.js应用程序中的开机自检功能。更新的C#控制台代码如下,仅需更改远程URL。

1: static void Main(string[] args)

2: {

3:
var key = args[0];

4:
var culture = args[1];

5:
var val = args[2];

6:

7:
var req =
HttpWebRequest.Create("http://nodejssample.azurewebsites.net/new");

8:
req.ContentType = "application/x-www-form-urlencoded";

9:
req.Method = WebRequestMethods.Http.Post;

10:

11:
var param =
string.Format("key={0}&culture={1}&val={2}", key, culture,
val);

12:
var bytes = System.Text.Encoding.UTF8.GetBytes(param);

13:
req.ContentLength = bytes.Length;

14:
using(var stream = req.GetRequestStream())

15: {

16:
stream.Write(bytes, 0, bytes.Length);

17:
}

18:

19:
var res = req.GetResponse();

20:
using (var sr = new StreamReader(res.GetResponseStream()))

21:
{

22:
Console.WriteLine(sr.ReadToEnd());

23:
}

24:
Console.ReadKey();

25: }

然后运行此应用程序,并在WASD中添加一些记录。

然后回到浏览器,并找到刚添加的条目。

总结

本文中,我演示了如何在Windows Azure Web Site上托管Node.js网站。这很简单,并易于用Git命令终端和新的GitHub GUI部署。

我也描述了如何从Windows Azure Web Site上的Node.js运用 Windows Azure SQL数据库。确保你导入了正确的sqlserver.node版本。为了更好地开发和云环境,我们可以用两种node-sqlserver模块,“node-sqlserver-x86”文件夹下的x86版本和“node-sqlserver-x64”文件夹下的x64版本。然后我们可以将x64版本导入我们本地开发中,并在提交同步Azure之前将其改为“node-sqlserver-x86”。

在Windows Azure中,除了SQL数据库之外,如 Storage、Service Bus等。几乎所有的Windows Azure服务都能通过一个叫做“azure”的模块被Node.js调用,该模块在Windows Azure Node.js开发包中。下一篇文章中我将演示如何将我们的Node.js应用程序托管在Windows Azure Cloud Service Worker Role上;如何使用存储服务;以及如何检索云服务配置。

本文翻译自:http://geekswithblogs.net/shaunxu/archive/2012/09/18/node.js-adventure---when-node.js-meets-windows-azure.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics