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

时间:2022-07-17 19:20:23

I am catching the following error when I try to manually run the app.js file directly from the Microsoft Azure Console. Why? The app.js works fine on my localhost. 将nodejs应用程序部署到Microsoft Azure时出错

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

This is my app.js file:

这是我的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);
  });
});

(The javascript code comes from swagger.io)

(javascript代码来自swagger.io)

1 个解决方案

#1


1  

You don't have to start the node.js application running on Azure Web Apps via executing the command node app.js manually on Azure. As you have already an entrance file app.js or server.js and an IIS web.config in the root directory of your application, you can directly browse your nodejs application via the URL.

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

I tested the example project at https://github.com/apigee-127/swagger-tools/tree/master/examples/2.0 which should be similar with yours. It works fine on my side.

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

Please try to browse your application directly on browser.

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

Any update, please feel free to let me know.

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

#1


1  

You don't have to start the node.js application running on Azure Web Apps via executing the command node app.js manually on Azure. As you have already an entrance file app.js or server.js and an IIS web.config in the root directory of your application, you can directly browse your nodejs application via the URL.

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

I tested the example project at https://github.com/apigee-127/swagger-tools/tree/master/examples/2.0 which should be similar with yours. It works fine on my side.

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

Please try to browse your application directly on browser.

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

Any update, please feel free to let me know.

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