2014-06-09 19 views
5

Sto facendo alcuni esperimenti con node.js + express e iisnode. Ho la seguente applicazione molto semplice, che si trova in C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0:Node.js + App basata su Express non in esecuzione con iisnode

app.js:

var express = require('express'); 
var app = express(); 
app.use(express.static(__dirname + "/public")); 

var port = process.env.PORT || 2709; 
app.listen(port, function() { 
    console.log('Listening on port ' + port); 
}); 

package.json:

{ 
    "name": "TestExpress", 
    "version": "0.0.0", 
    "private": true, 
    "dependencies": { 
    "express": "3.x" 
    } 
} 

web.config:

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

public/index.html:

<!doctype html> 
<html> 
<head> 
    <script language="javascript" type="text/javascript" src="js/jquery.js"></script> 
    <script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     console.log("READY!"); 
     $("#hello").html("Hello, World!"); 
    }); 
    </script> 
</head> 
<body> 
    <h1 id="hello" style="text-align:center;"></h1> 
</body> 
</html> 

La mia configurazione è: Windows 8.1, IIS 8; versione iisnode 0.2.11, versione del nodo v0.10.28.

Quando viene lanciato dalla riga di comando (C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0>node app.js), l'app viene eseguita come previsto: nel browser, vado a http://localhost:2709/ e vedo "Ciao, mondo!".

My IIS sta attualmente eseguendo altre applicazioni basate su node.js che non utilizzano Express da ubicazioni simili (ovvero da C:\tsapp-deploy\tsappsvr\appname\x.y.z\index.js), quindi presumo che sia configurato correttamente, ma quando provo a eseguire questa app dal browser, digitando http://localhost/tsappsvr/TestExpress/0.0.0/app.js Ottengo un 404 non trovato (in IE) o un "Impossibile ottenere /tsappsvr/TestExpress/0.0.0/app.js" in Chrome e Firefox.

Immagino che il problema potrebbe essere nel mio web.config, ma non riesco a capire come cambiarlo per far funzionare la mia app. Ho provato diverse modifiche a web.config come suggerito in altre risposte a domande simili, ma non ancora riuscito. Qualche suggerimento?

Grazie in anticipo.

risposta

3

Eric, hai installato il url-rewrite extension?

Ecco la configurazione che ho nel mio web.config:

<configuration> 
<system.webServer> 

    <handlers> 
    <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> 
    </handlers> 

    <rewrite> 
    <rules> 
     <rule name="myapp"> 
     <match url="/*" /> 
     <action type="Rewrite" url="app.js" /> 
     </rule> 
    </rules> 
    </rewrite> 
<system.webServer>  
<configuration> 
+0

Grazie David, non ho installato l'estensione di riscrittura dell'URL. Ora ce l'ho e ho aggiunto la regola di riscrittura a da web.config, ma ancora senza fortuna. Sono riuscito ad avere una risposta modificando il mio app.js come segue: 'var express = require ('express'); var http = require ('http'); var app = express(); app.use (funzione (req, res, next) { res.send ('Hello, World!'); }); var server = http.createServer (app); var port = process.env.PORT || 2709; server.listen (porta); ', ma quando provo qualcosa come' app.use ("/ static", express.static (__ dirname + '/ public')); 'la mia app continua a cadere nel middleware catch-all. – eca

11

credo di aver trovato una soluzione al mio problema:

  • Per prima cosa ho installato l'estensione URL-rewrite, grazie di nuovo, David.
  • In secondo luogo ho cambiato il mio web.config come segue:

web.config:

<configuration> 
    <system.webServer> 
    <handlers> 
     <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> 
    </handlers> 
    <rewrite> 
     <rules> 
     <rule name="myapp"> 
      <match url="/*" /> 
      <action type="Rewrite" url="app.js" /> 
     </rule> 
     </rules> 
    </rewrite> 
    </system.webServer> 
    <appSettings> 
    <add key="deployPath" value="/tsappsvr/TestExpress/0.0.0" /> 
    </appSettings> 
</configuration> 

Si prega di notare la chiave deployPath in appSettings sezione, il cui valore è impostato su percorso virtuale dell'app (set in IIS). Nel mio caso è /tsappsvr/TestExpress/0.0.0.

  • Infine, i miei app.js è ora la seguente:

app.JS:

// Preamble, so to say 
var express = require('express'); 
var http = require('http'); 
var app = express(); 

// Variable deployPath is set in web.config and must match 
// the path of app.js in virtual directory. 
// If app.js is run from command line: 
// "C:/tssapp-deploy/tsappsvr/TestExpress/0.0.0> node app.js" 
// deployPath is set to empty string. 
var deployPath = process.env.deployPath || ""; 

// Static content server 
app.use(deployPath + "/pages", express.static(__dirname + '/public')); 

// REST API handler (placeholder) 
app.all(deployPath + "/api", function(req, res) { 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end("<h2>REST API Handler found.</h2>"); 
}); 

// Default handler 
app.get(deployPath + "/", function(req, res) { 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end("<h2>Default Handler found.</h2>"); 
}); 

// Not Found handler 
app.use(function(req, res, next){ 
    res.writeHead(404, {'Content-Type': 'text/html'}); 
    res.write("<h2>The requested resource is not available.</h2>"); 
    res.write("Request received on port " + process.env.PORT + "<br>"); 
    res.write("Request URL: " + req.url + "<br>"); 
    res.write("Deploy Path: " + deployPath + "<br>"); 
    res.end('[iisnode version is ' + process.env.IISNODE_VERSION + ', node version is ' + process.version + ']'); 
}); 

// Server creation 
var server = http.createServer(app); 
var port = process.env.PORT || 2709; 
server.listen(port); 

variabili deployPath viene utilizzato come prefisso per tutti i gestori (ad eccezione del gestore "Not Found"). Quando app.js viene avviato da iisnode, deployPath fa parte di process.env, mentre non è definito se app.js viene avviato dalla riga di comando (ad esempio C:/tsapp-deploy/tsappsvr/TestExpress/0.0.0>node app.js). Ciò garantisce che app.js funzioni in entrambi i casi.

Ora, digitando http://localhost/tsappsvr/TestExpress/0.0.0/ nel browser spengo il gestore predefinito; Aggiunta di /pages all'ex URL Ho accesso a contenuto statico, mentre aggiungendo /api Attivo il gestore API REST. Lo stesso accade per http://localhost:2709/ come URL di base.

+0

Aggiunta di deployPath risolti per me. Grazie! – Beanwah

+0

Ho problemi a pubblicare il contenuto statico corrispondente all'interno delle directory public/javascripts o public/stylesheets. Sto usando app.use (express.static (path.join (__ dirname, deployPath, 'public'))); nel mio file app.js express – Robodude

+0

Calcolato: app.use (deployPath, express.static (path.join (__ dirname, 'public'))); – Robodude