【问题标题】:How to tell azure to run node server.js如何告诉 azure 运行 node server.js
【发布时间】:2019-03-18 18:59:50
【问题描述】:

我也有 angular 6 应用程序在 azure 上运行良好,但我需要为一些服务器变量添加 server.js 文件,我在 root 上添加了 server.js 文件,例如

Server.js

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
var router = express.Router();
const publicIp = require('public-ip');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist/projectname')));


app.get('/getip', (req, res) => {
  var ipcheck = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  (async () => {
    console.log(await publicIp.v4());
    res.json({
      ip: await publicIp.v4(),
      ip2: req.headers['x-forwarded-for'],
      ip3: req.connection.remoteAddress,
      ip4: req.ip
    });

  })();
});
// Send all other requests to the Angular app
app.get('/*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/projectname/index.html'));
});
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/prjectname/index.html'));
});


//Set Port
const port = process.env.PORT || '3000';
app.set('port', port);

const server = http.createServer(app);

server.listen(port, () => console.log(`Running on localhost:${port}`));

然后我转到 cmd 并运行 node server.js 它在本地主机上运行良好 但现在我想在 azure 服务器上做同样的事情如何部署它。 或者如何告诉 azure 总是 run node server.js

【问题讨论】:

  • 是否要将其部署在 Azure WebApp 上?或者其他像 VM 或 Functions 的东西?或者您只是想知道如何使用合适的服务将其部署到 Azure。
  • webapp 我也有发布设置,但不知道如何通过 nodejs 运行它

标签: node.js angular azure visual-studio-2017


【解决方案1】:

我假设您已经知道如何在 Azure 门户或Azure CLI 中创建 Azure WebApp。然后,我看到您在 Visual Stodio 2017 中开发了您的 Node.js 应用程序,并希望使用相同的工具将其部署到 Azure,因此我建议您可以参考 NTVS wiki 页面Publish to Azure Website using Web Deploy 了解如何操作。

接下来,在部署之前,您必须先了解以下四个注意事项。

  1. 请确认您创建的WebApp中的Node.js版本,默认版本为0.10+。你可以通过 url https://<your webapp name>.scm.azurewebsites.net/DebugConsole 和命令 node -v 访问你的 webapp 的 Kudo 控制台来获取它。您可以在 Azure CLI 中设置它以运行 az webapp config appsettings set --resource-group <your ResourceGroup> --name <app_name> --settings WEBSITE_NODE_DEFAULT_VERSION=10.14.1 之类的命令,或者在 Azure 门户中按照下图进行操作。有关 Azure WebApp 上 Node 版本的更多详细信息,请参阅我对 SO 线程 azure webapp webjob node version 的回答。

  1. 在您的项目中,必须有一个Web.config 文件来与 Azure WebApp 上的 IIS 对话如何启动您的 Node 应用程序。 Web.config文件的内容与Kudu wiki页面Using a custom web.config for Node apps中的示例相同,您可以直接复制它,因为您的引导js文件名也是server.js

  2. Azure WebApp 上的监听端口是由环境随机分配的,可以从环境变量HTTP_PLATFORM_PORT 中获取,process.env.PORT 也将获取。所以你不会在你的代码中为port做任何事情。

  3. 您的 Node 应用程序将部署到 Azure WebApp 的路径home/site/wwwroot,也可以通过 Kudu 控制台访问该路径。因此,无需任何工具,您也可以使用 Kudu 控制台部署您的应用,只需将wwwroot 中的文件结构与您的项目相同,包括所有 js 文件和目录,node_modulesweb.config

【讨论】:

    【解决方案2】:

    只需构建您的项目 ng build --prod 然后在 vs 17 的 dist 文件夹中打开项目文件夹,然后添加

    package.json

    index.js

    web.config

    您可以从here 中获取这 3 个文件,您可以根据需要编辑 index.js 我只需要添加这个

    app.get('/getip', (req, res) => {
      res.json({
          ip: req.headers['x-forwarded-for'].split(":")[0]
        });
    });
    
    app.get('*', (req, res) => {
       res.sendFile(path.join(__dirname, 'index.html'));
    });
    

    这通过 nodejs 运行我的应用程序。

    当我调用 getip 时,这给了我服务器 ip,否则返回处理所有路由的 angular index.html。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2014-06-05
      相关资源
      最近更新 更多