简体   繁体   中英

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. 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.

At the root of my site I have a folder called node_modules that has express in it. I also have a app.js file and a web.config file.

This is the web.config file:

<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:

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. The only files I'm accessing should be app.js and express and they are both there.

I'm very new to node.js and 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:

   <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. Apparently Express requires rewriting the urls instead of using the default document tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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