Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 885
curtwphillips Avatar asked Oct 21 '22 23:10

curtwphillips


1 Answers

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.

like image 64
curtwphillips Avatar answered Oct 27 '22 11:10

curtwphillips