2017-05-29 152 views

回答

1
  1. 安装IIS node
  2. 安装URL rewrite module
  3. <configuration>/<system.webServer>创建具有以下声明的Web.config:

    <handlers> 
        <add name="iisnode" path="server.js" verb="*" modules="iisnode" /> 
    </handlers> 
    
    <rewrite> 
        <rules> 
    
        <rule name="Server-side rendering" stopProcessing="true"> 
         <match url=".*" /> 
         <conditions logicalGrouping="MatchAll"> 
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
         </conditions> 
         <action type="Rewrite" url="server.js" /> 
        </rule> 
    </rules> 
    

您可以选择排除node_modules所以它们不会被IIS服务:

<security> 
    <requestFiltering> 
     <hiddenSegments> 
      <add segment="node_modules" /> 
     </hiddenSegments> 
    </requestFiltering> 
</security> 
  • 创建的node.js服务器,例如用express.js:从 '表示'

    const的应用程式=快递()

    应用

    进口明示。 get('*',(req,res)=> { /在这里呈现应用/ });

    app.listen(process.env.PORT);

  • 记得绑定到process.env.PORT,因为它通过IIS传递到您的服务器!

  • 渲染你的应用程序的服务器端,例如这样的: const App = ({message}) => <div>Hello {message}</div>

    const template = ...

  • (StackOverflow上吃我的嵌入JS代码...所以请复制并使用字符串插值)

    <!doctype html> 
     
    <html> 
     
        <head> 
     
         <meta charset="utf-8"> 
     
         <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     
        </head> 
     
         
     
        <body> 
     
         <div id="app">${body}</div> 
     
        </body> 
     
    
     
        <script async src="/bundle.js"></script> 
     
    </html>

    import { renderToString } from 'react-dom/server' 
    
    const renderedComponent = renderToString(<App message={`from ${req.url}`} />) 
    const html = template(renderedComponent) 
    res.send(html) 
    
  • 配置构建工具和路由器。
  • 您可以为Webpack创建两个独立的构建目标 - 一个用于客户端,另一个用于服务器。您还应该处理应用内路由,但这取决于您正在使用的库。

    2

    你仍然需要在你的服务器上有node.js。 IIS只会代理你的应用程序。

    有不同的方式如何实现这一目标:

    1)随着IISNode project

    说明你可以在这里找到: http://www.amazedsaint.com/2011/09/creating-10-minute-todo-listing-app-on.html

    2)随着ARR(应用程序请求路由)模块IIS :

    说明你可以在这里找到:https://adamtuttle.codes/add-node-to-existing-iis-server/

    +1

    很棒的回答。选项2允许我们运行反应开发服务器,其中包括实时重新加载等所有优点,但是在自定义域下(例如不在本地主机上)。 –

    相关问题