ExpressJS – Serve a Google Verification Page

My portfolio website is using Angular served by ExpressJS. For more than six months, I forgot to check my Google Webmaster tool and it shows that I don’t own the site anymore. What a shame! I have to verify the website again by serving Google’s verification page.

ExpressJS

In Express, it is straightforward to serve a static HTML file, however, I already served static files from my Angular build directory and I can’t put the Google verification page into that directory. I ended up serving the file directly.

Here is the stripped down version of my app.js file.

var express = require('express');
var path = require('path');
var app = express();

// My static files from Angular
app.use(express.static(path.join(__dirname, '/contents/dist')));

// Google verification page
app.get('/googlebf4c56384f8fc253.html', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/googlebf4c56384f8fc253.html'));
});

// Catch all route to service the Angular app
app.get('/*', function(req, res) {
    res.sendFile(path.join(__dirname + '/contents/dist/index.html'));
});

module.exports = app;

That’s it!

This entry was posted in Angular, NodeJS and tagged , . Bookmark the permalink.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *