2014-06-09 34 views
5

node.js + express ve iisnode ile bazı deneyler yapıyorum. ben C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0 bulunan aşağıdaki çok basit bir uygulama vardır:Node.js + Express tabanlı uygulama iisnode ile çalışmıyor

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> 

Yapılandırmam: Windows 8.1, IIS 8; iisnode sürüm 0.2.11, düğüm sürümü v0.10.28.

Komut satırından başlatıldığında (C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0>node app.js), uygulama beklendiği gibi çalışır: tarayıcıda http://localhost:2709/ adresine gidip "Merhaba, Dünya!" Bölümüne bakın.

Benim IIS şu anda benzer konumlardan (yani, C:\tsapp-deploy\tsappsvr\appname\x.y.z\index.js) ifade kullanmayan diğer node.js tabanlı uygulamaları çalıştırıyor, bu yüzden doğru yapılandırılmalı, ancak bu uygulamayı tarayıcıdan çalıştırmayı denediğimde farz ediyorum. http://localhost/tsappsvr/TestExpress/0.0.0/app.js Bir 404 Bulunamadı (IE'de) veya bir "Chrome/Firefox'ta" /tsappsvr/TestExpress/0.0.0/app.js GET. "

Sorunun web.config adresinde olabileceğini tahmin ediyorum, ancak uygulamamın çalışmasını sağlamak için bunu nasıl değiştireceğimi anlayamıyorum. Benzer soruların diğer cevaplarında önerildiği gibi web.config'te birkaç değişiklik denedim, ancak henüz başarı yok. Herhangi bir öneriniz var mı?

Şimdiden teşekkürler.

cevap

3

Eric, url-rewrite extension'u yüklediniz mi?

<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

Teşekkürler David, url-rewrite uzantısını yüklemedim. Şimdi buna sahibim ve yeniden yazma kuralını web.config ile ekledim, ancak henüz şansım olmadı. Sadece app.js'yi şu şekilde değiştirerek yanıt almayı başardım: 'var express = require ('express'); var http = gerekli ('http'); var app = express(); app.use (işlev (req, res, sonraki) { res.send ('Merhaba, Dünya!'); }); var server = http.createServer (app); var port = process.env.PORT || 2709 server.listen (bağlantı noktası); ', ama app.use ("/static ", express.static (__ dirname + '/ public')) gibi bir şey denediğimde;' app'im catch-all middleware'de kalıyor. – eca

11

benim soruna bir çözüm bulduk düşünüyorum: Burada

benim web.config var yapılandırma

  • Önce url-yeniden yazma uzantısı yüklü, teşekkürler yine David.

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> 

değeri uygulamanın sanal yolu olarak ayarlanır appSettings bölümünde deployPath anahtarı unutmayınız (set aşağıdaki gibi

  • İkincisi benim web.config değişti IIS'de. Benim durumumda /tsappsvr/TestExpress/0.0.0.

    uygulamayı şu şekildedir:

    • Son olarak, benim app.js şimdi.js: deployPath

      // 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); 
      

      Değişken ("bulunamadı" işleyici hariç) işleyiciler için bir ön ek olarak kullanılır. App.js iisnode'dan başlatıldığında, deployPath, process.env'un bir parçasıdır, app.js komut satırından başlatılırsa tanımlanmamıştır (ör. C:/tsapp-deploy/tsappsvr/TestExpress/0.0.0>node app.js). Bu, app.js'nin her iki durumda da çalışmasını sağlar.

      Şimdi tarayıcıda http://localhost/tsappsvr/TestExpress/0.0.0/ yazarak, varsayılan işleyiciyi çalıştırıyorum; /pages'u eski URL'ye eklemek, statik içeriğe erişimim var iken, /api eklerken REST API işleyicisini tetiklerim. Aynı durum, temel URL olarak http://localhost:2709/ için de geçerlidir.

    +0

    Benim için deployPath sabit şeyler ekleme. Teşekkürler! – Beanwah

    +0

    Genel/javascripts veya public/stylesheets dizinlerinin içindeki ilgili statik içeriği sunarken sorun yaşıyorum. app.use kullanıyorum (express.static (yol.join (__ dirname, deployPath, 'public'))); benim ekspres app.js dosyasımda – Robodude

    +0

    Figured it out: app.use (deployPath, express.static (yol.join (__ dirname, 'genel'))); – Robodude

    İlgili konular