简体   繁体   English

在Windows托管站点上运行基本node.js

[英]Running basic node.js on windows hosted site

I need some advice for getting a basic express app to work on my site. 我需要一些建议来获得一个基本的快速应用程序在我的网站上工作。 The site is hosted on Arvixe.com windows hosting. 该网站托管在Arvixe.com上。 I can run code on my localhost and it works but as soon as I move it to the Arvixe site it is giving me a 404 error - file or directory not found. 我可以在我的localhost上运行代码并且它可以工作,但是一旦我将它移动到Arvixe站点,它就会给我一个404错误 - 找不到文件或目录。

At the root of my site I have a folder called node_modules that has express in it. 在我的网站的根目录中,我有一个名为node_modules的文件夹,其中包含express。 I also have a app.js file and a web.config file. 我还有一个app.js文件和一个web.config文件。

This is the web.config file: 这是web.config文件:

<configuration>
  <system.webServer>
    <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
    <defaultDocument enabled="true">
        <files>
            <add value="app.js" />
        </files>
    </defaultDocument>
  </system.webServer>
</configuration>

This is the app.js file: 这是app.js文件:

var express = require('express');
var app = express();

app.get('/', function(request, response) {
    response.send("This would be some HTML");
});

app.get('/api', function(request, response) {
    response.send({name:"Raymond",age:40});
});
app.listen(process.env.PORT);

The only difference between the localhost version and this version is there is a web.config file and in app.js I changed app.listen to use PORT instead of a local port number. localhost版本与此版本之间的唯一区别是有一个web.config文件,在app.js中我更改了app.listen以使用PORT而不是本地端口号。 The only files I'm accessing should be app.js and express and they are both there. 我正在访问的唯一文件应该是app.js和express,它们都在那里。

I'm very new to node.js and express. 我是node.js和express的新手。 Am I missing something about how these files communicate between one another or what is happening to stop my site from running? 我错过了一些关于这些文件之间如何相互通信或者发生了什么阻止我的网站运行的问题? Is there a simple way I could debug things like this in the future? 有一种简单的方法可以在将来调试这样的东西吗?

I found a way to get this to work. 我找到了一种让它发挥作用的方法。 I added this code to my web.config: 我将此代码添加到我的web.config:

   <rewrite>
      <rules>
        <rule name="/">
          <match url="/*" />
          <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite> 

I'm surprised this was necessary because app.js was set to the default document. 我很惊讶这是必要的,因为app.js被设置为默认文档。 Apparently Express requires rewriting the urls instead of using the default document tags. 显然,Express需要重写网址而不是使用默认文档标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM