【问题标题】:node web app in azure is blankazure 中的节点 Web 应用程序为空白
【发布时间】:2016-01-31 19:29:11
【问题描述】:

我是 node 和 express 的新手,但是当我在 windows azure 中托管我的 node web 应用程序时遇到了一个问题,顺便说一下,它在 localhost 上完全可以正常工作。我只是得到一个空白屏幕。

这是我的: server.js

var root = require('root');
var github = require('github-auth');
var express = require('express');
var path = require('path');

// var app = root();
var app = express();


var gh = github('clientid', 'clientsecret, {
  organization: 'my-org',
  team: 'my-team',
  autologin: true // This automatically redirects you to github to login
});


app.get('/login', gh.login);


app.use(express.static(__dirname + '/'));


app.all('*', gh.authenticate);
app.all('*', function(req, res, next) {
    if (!req.github) return res.sendFile(__dirname + '/login.html');


  if (!req.github.authenticated) res.sendFile(path.join(__dirname+'/kickout.html'));
  next();
});

app.get('/main',function(req,res){
  res.sendFile(path.join(__dirname+'/main.html'));
});

app.get('/about',function(req,res){
  res.sendFile('/kickout.html');
});

app.listen(3000);

console.log("Running at Port 3000");

【问题讨论】:

标签: node.js azure express deployment


【解决方案1】:

在 Azure Web Apps Service 上运行的 Node.js 应用程序托管在通过 IISNode 处理的 IIS 映射上,这提供了一个 Named Pipe 来接收传入请求,而不是像在本地运行时那样使用的 TCP 端口。

Named Pipe 已在 Azure Web 应用程序的 Node.js 运行时中定义为 port。您可以在您的应用链接中定义portprocess.env.PORT || 3000,您的应用可以通过它在 Azure 或本地运行。

你可以检查你的根目录下是否有一个文件web.config,这是你的应用程序的IIS的配置。它应该有类似的内容:

<configuration>
     <system.webServer>
          <handlers>
               <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
               <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
          </handlers>
          <rewrite>
               <rules>

                    <!-- Don't interfere with requests for node-inspector debugging -->
                    <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                        <match url="^server.js\/debug[\/]?" />
                    </rule>

                    <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                    <rule name="StaticContent">
                         <action type="Rewrite" url="public{REQUEST_URI}"/>
                    </rule>

                    <!-- All other URLs are mapped to the Node.js application entry point -->
                    <rule name="DynamicContent">
                         <conditions>
                              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                         </conditions>
                         <action type="Rewrite" url="server.js"/>
                    </rule>

               </rules>
          </rewrite>
          <!-- You can control how Node is hosted within IIS using the following options -->
        <!--<iisnode      
          node_env="%node_env%"
          nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;"
          nodeProcessCountPerApplication="1"
          maxConcurrentRequestsPerProcess="1024"
          maxNamedPipeConnectionRetry="3"
          namedPipeConnectionRetryDelay="2000"      
          maxNamedPipeConnectionPoolSize="512"
          maxNamedPipePooledConnectionAge="30000"
          asyncCompletionThreadCount="0"
          initialRequestBufferSize="4096"
          maxRequestBufferSize="65536"
          watchedFiles="*.js"
          uncFileChangesPollingInterval="5000"      
          gracefulShutdownTimeout="60000"
          loggingEnabled="true"
          logDirectoryNameSuffix="logs"
          debuggingEnabled="true"
          debuggerPortRange="5058-6058"
          debuggerPathSegment="debug"
          maxLogFileSizeInKB="128"
          appendToExistingLog="false"
          logFileFlushInterval="5000"
          devErrorsEnabled="true"
          flushResponse="false"      
          enableXFF="false"
          promoteServerVars=""
         />-->
        <iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade"/>
     </system.webServer>
</configuration>

【讨论】:

    【解决方案2】:

    通过 Azure Web 应用托管 Node.js 应用时,您需要修改端口初始化,如以下简化示例所示:

    var http = require('http'),
        port = process.env.PORT || 1337;
    
    http.createServer(function(req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Hello World\n');
    }).listen(port);
    

    在本地运行时,将使用文字端口(例如上面的 1337),但在 Azure 中运行时,托管平台将分配一个端口到 process.env.port,并将使用该端口。您可以找到有关部署 Node.js 应用程序的完整演练here。在 WebApp 中运行时,一种称为 IIS Node 的技术用于运行您的 Node 应用程序。更多详情请见here

    【讨论】:

    • 非常感谢,我所要做的就是配置我的端口,Azure 将分配给process.env.port
    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2016-04-22
    • 2023-04-05
    • 2017-03-20
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多