Arti
Arti

Reputation: 7772

prerender.io not working with expressjs and angularjs

I have setup a server with Express.js:

const env = process.env.ENV || 'PROD';
const port = process.env.PORT || 8080;
const express = require('express');
const app = express();

if(env === 'PROD') {
    app.all('*', function(req, res, next) {
        var protocol = req.headers['x-forwarded-proto'];
        if(protocol && protocol === 'http') {
            res.redirect(301, 'https://' + req.headers.host + req.url);
        }
        return next();
    });
}

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

app.use(express.static(__dirname + '/app/'));

app.get('*', function(req, res){
    res.sendFile(__dirname + '/app/index.html');
});

app.listen(port);
console.log("App started on port "+port);

In angularjs app set config:

config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
    $locationProvider.html5Mode(true);
}])

In index.html:

<meta name="fragment" content="!">

As documentation says: https://prerender.io/documentation/test-it

I do:

http://mydomainm/user/1?_escaped_fragment_=

But my page not cached. enter image description here

Upvotes: 1

Views: 619

Answers (2)

Arti
Arti

Reputation: 7772

So it looks like the middleware is working properly but you are sending us an http URL so we are returning a 301. Can you modify your middleware to be like this:

app.use(require('prerender-node').set('prerenderToken', 'mytoken').set('protocol', 'https'));

That should fix that redirect issue and get everything working properly.

Upvotes: 2

Prerender.io
Prerender.io

Reputation: 1604

That looks like it should be working properly. Can you send an email to [email protected] so that we can check your Crawl Stats and do some testing on our end? Thanks!

Upvotes: 0

Related Questions