简体   繁体   中英

Is it possible to execute a C# API backend and Node.JS frontend on the same Azure AppService instance

We have a frontend running React server off a Node.JS server that is talking to a backend running as a.Net 5 web service.

The services are running fine when placed on two Azure App Service instances, however, we are interested in simplifying deployment by having the two run on the same App Server instance.

Is this possible, or should we just move on?

In our efforts, we have the following web.config cobbled together based on merging different ideas we have found. It is basically serving Node.JS through iisnode whereas the /api-calls are directed to the.Net DLL.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <webSocket enabled="false" />
        <handlers>
            <remove name="aspNetCore"/>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
            <rules>
                <!-- Serve static files directly -->
                <rule name="StaticContent">
                    <action type="Rewrite" url="public{REQUEST_URI}"/>
                </rule>
            
                <!-- All calls not going to "/api" or "/apiv2" are sent to server.js -->
                <rule name="DynamicContent">
                    <match url="(api|apiv2)" negate="true" />
                    <action type="Rewrite" url="server.js"/>
                </rule>
            </rules>
        </rewrite>
        <aspNetCore processPath="dotnet" arguments=".\Backend.Api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"/>
    </system.webServer>
</configuration>

we updated our server.js to the following simple setup to avoid any extra problems;

const express = require('express');
const server = express();

// We need to get the port that IISNode passes into us 
// using the PORT environment variable, if it isn't set use a default value
const port = process.env.PORT || 3000;

// Setup a route at the index of our app    
server.get('/', (req, res) => {
    return res.status(200).send('Hello World');
});

server.listen(port, () => {
    console.log(`Listening on ${port}`);
});

In our Azure App Service we are running.Net Stack and.Net 5 major and minor version. We have not Startup command. The App Service Plan is running Linux.

To summarize, the API is working fine, it is the iisnode/Node.JS server that is not working as expected giving 404 errors. We assume the complete Node.JS is not even starting.

Call to the route give error message like so:

{"type":"https://httpstatuses.com/405","title":"Method Not Allowed","status":405,"traceId":"xxx"}

Whereas calls to any other URL than /api /api2 give:

{"type":"https://httpstatuses.com/404","title":"Not Found","status":404,"traceId":"xxx"}

We are not seeing any errors in the server logs.

On Windows OS, IIS enables you to set up many virtual applications inside of a single website if you select the Windows Web App option. To use this strategy, you could follow the Multiple applications deployment in a single azure app service provided by Microsoft.

Note : The same application pool would be shared by the several virtual applications.

Or It might be CORS in the app.js backend file is not enabled ad that is stopping to access the backend by frontend.

You can try the below steps if it helps to fix the issue:

  1. Set up a separate app service for front-end applications like Angular, React, Node JS and others.
  2. From the frontend application, call the Node JS APIs.

Or,

A gulp file is used to build up and operate the client and server on the same server, and the project is then deployed to an app service. In that situation, no cross-site communication occurs. This is the one that is most favored.

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