Im trying to implement prerender.io for my Angular 1.6.0 app running on a native Node.js server
The documentation for setting up the middleware makes use of the connect middleware and specifically cites Express.js
app.use(require('prerender-node').set('prerenderToken', 'TOKEN'));
I am not using Express and was not using the Connect middleware to run my server.
My server.js is as follows:
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) How can I implement prerender.io with this configuration?
2) I did actually install Connect
and trying to implement the middleware as follows:
var conn = connect();
conn.use(require('prerender-node').set('prerenderServiceUrl','http://localhost:3000/').set('prerenderToken', 'lqnF62jXABouJiFA2SuA'));
Which I just appended after the server code above.
I am not getting any errors but I do not see that anything at localhost:3000 after running node server
. Although, my app runs fine on localhost:8080
How can I get prerender.io set up on this server?
You might could do something like the following, but I'd suggest breaking each function out into its own function to prevent all of the callbacks. Just wanted to leave the code alone so you could see where it was changed.
As for your second question, you might have it installed correctly. Do you see a request on your prerender server console if you go to:
http://localhost:8080/?_escaped_fragment_=