0

I am trying to deploy my MEAN framework application on IIS using iisnode on local machine.

My api call is working after hosting on IIS, but web application page is not loading because it can not load required css and js files related to web app.

Link containing image showing my project structure

My server.js contains following code:-

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var mongoOps = require('./MongoOperations.js');
var googleAuthOps = require('./passport_google.js');
var bodyparser = require('body-parser');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var cookieParser = require('cookie-parser');
var session = require('express-session');

exports.io = require('socket.io')(http);
var port = process.env.port || 1337;

app.use(bodyparser.json()); // for parsing application/json
app.use(bodyparser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(session({ secret: 'this is the secret' }));//, resave: true, saveUninitialized: true }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());


console.log("In Express top");


app.use(express.static(__dirname + '/views/src'));

console.log("In Express 11");


passport.serializeUser(function (user, done) {
    done(null, user);
});

passport.deserializeUser(function (user, done) {
    done(null, user);
});

app.post("/login", passport.authenticate('local'), function (req, res) {
    console.log('user' + req.user);
    var user = req.user;
    console.log(user);
    res.json(user);
});

app.get('/loggedin', function (req, res) {
    res.send(req.isAuthenticated() ? req.user : '0');
});

app.post('/logout', function (req, res) {
    req.logout();
    res.redirect('/login');
    //res.send(200);
});


app.get('/auth/google', passport.authenticate('google',
    {
        hd: 'gslab.com',
        scope: ['https://www.googleapis.com/auth/userinfo.profile',
            'https://www.googleapis.com/auth/userinfo.email']
    }),
    function (req, res) { } // this never gets called
);

app.get('/auth/google/callback', passport.authenticate('google',
    { successRedirect: '/#/dashboard', failureRedirect: '/#/login' }//this may change when website published to logout from all google services https://accounts.google.com/logout
));

app.use(function (req, res, next) {
    if (!req.user)
        res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    next();
});



var auth = function (req, res, next) {
    if (!req.isAuthenticated())
        res.send(401);
    else
        next();
};

app.get('/', function (request, response) {
    response.sendfile("views/src/index.html");
});

app.get('/idp1/api/idcnt', mongoOps.fetchIdeaCount);

app.get('/idp1/api/reclimit/:p_num', mongoOps.fetch);
app.post('/idp1/api/results', mongoOps.add);
//app.put('/idp1/api/results/:resultId', mongoOps.modify); //for later use

app.put('/idp1/api/results/:resultId', mongoOps.modify2);


app.get('/idp1/api/myreclimit/:uid/:pNum', mongoOps.fetchMyIdeas);
app.get('/idp1/api/myidcnt/:uid', mongoOps.fetchMyIdeaCount);
app.put('/idp1/api/updateidea/:iid', mongoOps.modifyIdea);

//Question master related routes
app.post('/idp1/api/question', mongoOps.upsertQuestion);
app.get('/idp1/api/question', mongoOps.getAllQuestion);
app.get('/idp1/api/question/:questionId', mongoOps.getQuestion);
app.delete('/idp1/api/question/:questionId', mongoOps.deleteQuestion);

//Role master related routes
app.post('/idp1/api/role', mongoOps.upsertRole);
app.get('/idp1/api/role', mongoOps.getAllRole);
app.get('/idp1/api/role/:roleId', mongoOps.getRole);
app.delete('/idp1/api/role/:roleId', mongoOps.deleteRole);

//Access rights related routes
app.get('/idp1/api/accessright', mongoOps.getAllAccessRight);

//Technology stuff
app.get('/idp1/api/techResults', mongoOps.fetchTechnology);
app.post('/idp1/api/techResults', mongoOps.addTechnology);
app.delete('/idp1/api/techResults/:id', mongoOps.delTechnology);
app.put('/idp1/api/techResults/:id', mongoOps.editTechnology);
app.get('/idp1/api/techResults/:id', mongoOps.getData);
app.post('/idp1/api/techResults', mongoOps.editTechnology);

app.get('/idp1/api/catresults', mongoOps.fetchCat);


app.get('/idp1/api/commentspnid/:iid/:pn', mongoOps.fetchCom2); //to fetch comments after pagination

app.get('/idp1/api/comments/count/:ideaId', mongoOps.fetchComCount); //to fetch comments count

if (app.post('/idp1/api/practice', mongoOps.addPractice)) { console.log("in practice post if"); }


app.put('/idp1/api/comments', mongoOps.modifyCom);

app.put('/idp1/api/updatecommentsNotify/:ideaId', mongoOps.resetCommentNotificationCount)


app.get('/idp1/api/getUserId/:userEmail', mongoOps.fetchUserData);
app.get('/idp1/api/userdetails/:userEmail', mongoOps.fetchUserData);
app.put('/idp1/api/userdetailstoupdate/:userId', mongoOps.updateUserData);
app.post('/idp1/api/userdetails', mongoOps.addUser);

app.post('/idp1/api/upsert_category', mongoOps.upsertCategory);
app.get('/idp1/api/fetch_categories', mongoOps.fetchCat);
app.get('/idp1/api/fetch_category/:categoryId', mongoOps.getSingleCat);
app.put('/idp1/api/delete_category/:categoryId/:userId', mongoOps.deleteCategory);



app.use('/idp11', express.static(path.join(__dirname, 'views/src/components')));
app.use('/idp2', express.static(path.join(__dirname, 'views/dist/js')));
app.use('/idp3', express.static(path.join(__dirname, 'views/src/img')));
app.use('/idp4', express.static(path.join(__dirname, 'views/src/templates')));
app.use('/idp5', express.static(path.join(__dirname, 'views/src/js')));
app.use('/idp6', express.static(path.join(__dirname, 'views/src')));

http.listen(port);

And the web.config file is as follows:-

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>

        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <!-- All other URLs are mapped to the node.js site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="server.js"/>
        </rule>
      </rules>
    </rewrite>

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled

      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->
  </system.webServer>
</configuration>

We hosted the project on azure and it is working fine. Also our azure web app resource is using iisnode. But on local machine when we deploy on IIS with help of iisnode we get following error :

error.

byJeevan
  • 3,728
  • 3
  • 37
  • 60

1 Answers1

0

The error that you got tells you that your configuration is invalid.

Make sure that you configure the IIS correctly. See the examples at:

There are a lot of good resources and config examples there.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • 1
    Though I'm not suggesting you're wrong. IMO, This is closer to a comment than a solution. In general, saying things like "See examples and try to follow", "Read the documentation" and "I don't know what your problem is so follow the tutorial" are **not** answers. – Wajih Aziza Jul 06 '17 at 05:52