Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep getting "Cannot GET" error with iisnode and Express #160

Closed
mfcollins3 opened this issue May 9, 2012 · 4 comments
Closed

Keep getting "Cannot GET" error with iisnode and Express #160

mfcollins3 opened this issue May 9, 2012 · 4 comments

Comments

@mfcollins3
Copy link

I created a basic Express web application and set up my web.config file like the documentation shows:

<configuration>
    <system.webServer>
        <handlers>
            <add name=iisnode" path="myapp.js" verb="*" modules="iisnode"/>
        </handlers>
    </system.webServer>
</configuration>

I'm developing on Windows 7 and have my node application hosted in the root directory of an IIS web site.

When I enter the URL http://localhost:8888/myapp.js, I get the following:

Cannot GET /myapp.js

myapp.js is pretty basic:

var express = require('express');
var routes = require('routes');

var app = module.exports = express.createServer();
app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
});

app.configure('development', function() {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    });
});

app.configure('production', function() {
    app.use(express.errorHandler());
});

app.get('/', routes.index);

port = process.env.PORT || 3000;
app.listen(port, function() {
    console.log("Server listening on port " + port + " in " + app.settings.env + " mode.");
});

If I run http://localhost:8000/myapp.js/debug, node inspector opens, but I don't see myapp.js in the list of loaded scripts. I see all of the express scripts and my routes/index.js script though.

Am I missing something or any hints on what to look for to fix it?

Thanks in advance.

@tjanczuk
Copy link
Owner

tjanczuk commented May 9, 2012

The immediate cause of the issue is that you don't seem to have a route registered for the '/myapp.js' path in your express app. However, the big picture is that in case of a node.js app hosted in iisnode, and Express app in particular, you probably want to have the entire URL space redirected to the entry point of your application. For that, you need to use URL rewriting in IIS.

I suggest starting with the following boilerplate web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>         
      <handlers>
           <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
     </handlers>
      <rewrite>
           <rules>

                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                     <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$"/>
                </rule>

                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                    <match url="^server.js\/debug[\/]?" />
                </rule>

                <rule name="StaticContent">
                     <action type="Rewrite" url="public{REQUEST_URI}"/>
                </rule>

                <rule name="DynamicContent">
                     <conditions>
                          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                     </conditions>
                     <action type="Rewrite" url="server.js"/>
                </rule>

           </rules>
      </rewrite>
   </system.webServer>
 </configuration>

What this does is the following:

  • assumes server.js is the entry point to your node.js application that will receive HTTP requests for all URL paths except:
  • requests for logs (/server.js.logs/0.txt, /server.js.logs/1.txt, etc.),
  • debugging requests (/server.js/debug),
  • requests for physical files that exist in the public subdirectory (e.g. request for /styles.css will be handled by the static file handler in IIS rather than your node.js application IFF that file exists at the \public\styles.css location).

Requests for all other URLs (e.g. /a/b/c?foo=12) will now be sent to the server.js application and will be handled following the logic implemented there. In case of an Express app, express routes will apply.

@mfcollins3
Copy link
Author

Thank you very much for your quick response.. The web.config seems to have fixed my issues.

@jeevjyot
Copy link

In my Case do i have to write the rewrite rules for all the paths or only for main files. Having hard time putting node js application on IIS server. thank you

var mysql= require('mysql');
var favicon = require('serve-favicon');
var api = require('./routes/api');
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();

//app.use is to define the pages and folder and basic usage
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(__dirname + '/www/public'))
app.use('/view', express.static(__dirname + '/www/views/viewer'));
app.use('/', express.static(__dirname + '/www/views/models'));
app.use(favicon(__dirname + '/www/public/images/favicon1.ico'));
app.use('/api', api);

app.use(bodyParser.json());

app.set('port', process.env.PORT || 3000); //setting server port to 3000

var query="select label,urn from lmvmodeloption"; //to retrive the urn and label from lmvmodeloption

//Mysql Connection
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password :'',
database: 'xx_xx',
port: '3307',
});

//Get Function to check
app.get('/data', function(req, res){
res.send('hello world'); //replace with your data here
});

/
app.get('/lmvmodels',function(req,res){
connection.query(query,function(err,rows,fields){
if(!err)
res.send(rows);
else
console.log(err.stack);
});
});

//Logout function
app.post('/logout',function(req,res){
console.log('Im logging out ');
req.session = null;
res.send('logout');
});

n
app.post('/endpoint',function(req,res){
var user_name=req.body.user1;
var password=req.body.password;
//var d={ux:user_name,dx:password};
console.log("User name = "+user_name+", password is "+password);
var s='INSERT INTO lmvmodeloption(label,urn) '+
'VALUES("'+password+'","'+user_name+'")';
console.log(s);
connection.query(s,function(err,res){
if(err)
throw err;
});
res.end("yes");
});
app.use(cookieParser())
app.use(session({secret: 'Keyboard car'}));
//this function takes care of the login, check if it returns the rows, if yes send success message, login success
app.post('/login',function(req,res){
var p_wt=req.body.pass_w;
var u_wt=req.body.user_w; var sess;
//console.log(p_wt);
//console.log(u_wt);
var q= 'select email, password from user_login where email ="' +u_wt+ '" and password = "' +p_wt+ ' "';
connection.query(q,function(err,rows,fields){
//console.log(rows[0].email);
if(rows.length>0){
sess=req.session;
sess.email_u=rows[0].email;
console.log("Session="+ sess.email_u);
console.log(rows);
//console.log("success sent");
console.log(rows[0].email);
res.send("success");
}
else{
res.send("Bad Data");
console.log("bad data")
// console.log(rows);
}
});

});

//Server is Starting on port 3000
var server = app.listen(app.get('port'), function() {

connection.connect(function(err){
    if(err) {
        console.log('error connecting to the database' + err.stack);
        return;
    }
});

/* connection.query('SELECT email as first from user_login where email="jeevjyot.chhabda@whiting-turne"', function(err, rows, fields) {
if (!err)
console.log(rows);
else
console.log('Error while performing Query., Empty it is, no rows'+ err.stack);
});*/
console.log('Server listening on port ' +
server.address().port);
});

//this function checks if no session is set, redirect the user to the index page
function checkAuth(req,res,nect){
if(!sess.email_u){
res.redirect("/");
}
}

@jayakumar4u
Copy link

jayakumar4u commented Sep 6, 2019

Hi @tjanczuk,

I follow your above instruction, but still, I cannot retrieve my logged username, please help to resolve.

My code below:
Config.xlm:

image

app.js:

image

Result:

image

IIS:
image

I got empty username and authenticationType, any thing i want to do in the code or iis config?
please help to resolve

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants