在aure上使用express.js部署react app

时间:2018-06-06 10:11:22

标签: node.js reactjs amazon-web-services azure azure-web-sites

我的server.js看起来像这样:

const express = require('express');
var path = require('path');
var buildDir = path.join(__dirname, '/build');
const app= express();
app.use(express.static('build'));

app.get('*', function (req, res) {
    res.sendFile(buildDir + '//index.html');
    console.log(buildDir);
  });

const port = process.env.PORT || '90';
app.listen(port, () => {
  console.log('We are live on ' + port);
});

Package.json看起来像这样:

{
  "name": "express_js_example",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "prestart": "npm install"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3"
  }
}

Web.config看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>   
        <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>    
    <httpErrors errorMode="Detailed"></httpErrors>     
    </system.webServer>
</configuration>

文件夹结构: Folder structure

我的问题是:当我将这些文件部署到azure Web服务时。它给我一个错误,说 IIS无法访问网站或应用程序的web.config文件。我认为问题是,它找不到express.js。我这样做是对的吗?但是,当我向我的server.js添加以下代码时,它完美无缺。

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

我是否还必须部署 node_modules 文件夹? 我希望你理解我的问题。请有人帮助我。

0 个答案:

没有答案