将nodejs应用程序部署到Microsoft Azure时出错

时间:2016-08-30 15:55:29

标签: node.js azure

当我尝试直接从Microsoft Azure控制台手动运行app.js文件时,我发现以下错误。为什么? app.js在我的localhost上工作正常。 enter image description here

这是我的app.js文件:

'use strict';

var app = require('connect')();
var http = require('http');
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var fs = require('fs');
var serverPort = process.env.PORT || 3000;

// swaggerRouter configuration
var options = {
  swaggerUi: '/swagger.json',
  controllers: './controllers',
  useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync('./api/swagger.yaml', 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  // Start the server
  http.createServer(app).listen(serverPort, function () {
    console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
    console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
  });
});

(javascript代码来自swagger.io)

1 个答案:

答案 0 :(得分:1)

您不必通过在Azure上手动执行命令node app.js来启动Azure Web Apps上运行的node.js应用程序。由于您的应用程序根目录中已有入口文件app.jsserver.js以及IIS web.config,因此您可以通过URL直接浏览nodejs应用程序。

我测试了https://github.com/apigee-127/swagger-tools/tree/master/examples/2.0的示例项目,该项目与您的类似。它在我身边很好。

请尝试直接在浏览器上浏览您的应用程序。

任何更新,请随时告诉我。