2017-08-28 41 views
0

我試着去實現prerender.io對於本機的Node.js服務器上運行我的角度1.6.0應用prerender.io與當地的Node.js

設立中間件的文檔利用了connect middleware和具體列舉Express.js

app.use(require('prerender-node').set('prerenderToken', 'TOKEN'));

我不使用Express和未使用連接中間件運行我的服務器。

我server.js如下:

var app = http.createServer(function(req, res){ 

var filePath = './debug/index.html'; 
var uri = req.url; 

// Load index.html only when uri is not referencing a sub-directory of ./www (and is thus a URL) 
for(i in dir){ 
    if(uri.includes('/'+dir[i])) { 
     filePath = './debug'+uri; 
     break; 
    } 
} 

fs.exists(filePath, function(exists) { 

    if(exists){ 

     fs.readFile(filePath, function(err, html) { 

      if(err){ res.writeHead(500); res.end(); } 
      else {   

       var ext = path.extname(filePath); 
       var contentType = 'text/html'; 
       switch(ext) { 
        case '.js': 
         contentType = 'text/javascript'; 
         break; 
        case '.css': 
         contentType = 'text/css'; 
         break; 
        case '.jpg': 
         contentType = 'image/jpeg'; 
         break; 
        case '.png': 
         contentType = 'image/png'; 
         break; 
        case '.svg': 
         contentType = 'image/svg+xml'; 
         break; 
        case '.pdf': 
         contentType = 'application/pdf'; 
         break; 
        default: contentType = 'text/html'; 
       } 

       res.writeHead(200, { 'Content-Type': contentType }); 
       res.end(html, 'utf-8'); 
      } 

     }); 

    } else { 

     res.writeHead(404); 
     res.end(); 
    } 
}); 

}).listen(port, function(){ 

    console.log('server is running on port '+port); 

}); 

1)我怎樣才能實現prerender.io此配置?

2)我沒有實際安裝Connect並試圖實現中間件如下:

var conn = connect(); 
conn.use(require('prerender-node').set('prerenderServiceUrl','http://localhost:3000/').set('prerenderToken', 'lqnF62jXABouJiFA2SuA')); 

我在上面的服務器代碼後僅追加。

我沒有收到任何錯誤,但在運行node server後,我沒有在localhost:3000上看到任何東西。雖然,我的應用在本地主機上運行良好:8080

如何在此服務器上設置prerender.io?

回答

0

你可能會做類似於下面的事情,但我建議將每個函數分解到它自己的函數中以防止所有的回調。只想保留代碼,以便可以看到它發生了什麼變化。

var prerender = require('prerender-node').set('prerenderToken', 'TOKEN'); 
var app = http.createServer(function(req, res){ 

prerender(req, res, function() { 

    var filePath = './debug/index.html'; 
    var uri = req.url; 

    // Load index.html only when uri is not referencing a sub-directory of ./www (and is thus a URL) 
    for(i in dir){ 
     if(uri.includes('/'+dir[i])) { 
      filePath = './debug'+uri; 
      break; 
     } 
    } 

    fs.exists(filePath, function(exists) { 

     if(exists){ 

      fs.readFile(filePath, function(err, html) { 

       if(err){ res.writeHead(500); res.end(); } 
       else {   

        var ext = path.extname(filePath); 
        var contentType = 'text/html'; 
        switch(ext) { 
         case '.js': 
          contentType = 'text/javascript'; 
          break; 
         case '.css': 
          contentType = 'text/css'; 
          break; 
         case '.jpg': 
          contentType = 'image/jpeg'; 
          break; 
         case '.png': 
          contentType = 'image/png'; 
          break; 
         case '.svg': 
          contentType = 'image/svg+xml'; 
          break; 
         case '.pdf': 
          contentType = 'application/pdf'; 
          break; 
         default: contentType = 'text/html'; 
        } 

        res.writeHead(200, { 'Content-Type': contentType }); 
        res.end(html, 'utf-8'); 
       } 

      }); 

     } else { 

      res.writeHead(404); 
      res.end(); 
     } 
    }); 
}); 

}).listen(port, function(){ 

    console.log('server is running on port '+port); 

}); 

至於你的第二個問題,你可能會正確安裝它。您是否在預渲染服務器控制檯上看到了請求:

http://localhost:8080/?_escaped_fragment_=