简体   繁体   中英

Consume node.js in ASP.NET MVC View

I'm .Net developer and new bie to Node.js application. I was trying to create a proxy layer for my ASP.NET MVC application through Node.js to do cross domain communication without touching the server level configuration of my ASP.NET WebAPI.

Lets start from code to quickly understand my questions,

I have created a single MVC application that holds my Node under "MyApp/app.js" and ASP.NET MVC view that will make an AJAX call to my Node file.

<h2>This is an Index page</h2>

<script>
    $(function () {
        $.ajax({
            url: "/sample",
            success: function () {
                alert('Done');
            }
        });
    });
</script>

Below is my web.config,

<handlers>      
      <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
      <add name="iisnode" path="MyApp/app.js" verb="*" modules="iisnode" />      
    </handlers>

Here is my app.js,

var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);//When I hardcode to Port:80 I'm getting "Error: listen EACCES 0.0.0.0:80"

app.listen(app.get('port'), function () {
    console.log('Express up and listening on port' + app.get('port'));
});

app.get('/Sample', function (req, res) {
    res.send('Hello World Sample!');
});

When I try to open my view, the AJAX call was failing with "404 error". I tried to run my Node app in port :80 but it throws err "Error: listen EACCES 0.0.0.0:80". Why can't I run my Node app and ASP.NET MVC view on same port?

Unfortunately it's not possible to run two applications on the same port. That will be why you're getting the port error when running the Node application.

Is there a reason why you're trying to do this in the same application?

What you seem to be describing is creating an API using Node and accessing that from a website written on top of ASP.NET MVC?

If this is the case you'll need to run them either on different IP addresses or different servers in order to share the same port - or simply run them on different ports.

The reason for your 404 error is twofold:

  1. You need to add a rewrite rule beneath your handler definiton so that IIS will forward all of your calls to app.js

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

  2. Your node.js request urls will contain the full path requested, but the route used for matching /Sample will begin with (in accordance with the example url rewrite above) /appjs , or possibly more than that, eg /iisAppName/appjs/sample . I know restify can match routes with regex, if express does too then you'd use something like

    app.get({ path: /.*/Sample$/, ...

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