DEV Community

Cover image for Vuejs app on CleverCloud + HTTPS redirection in few minutes
Steeve
Steeve

Posted on • Updated on

Vuejs app on CleverCloud + HTTPS redirection in few minutes

Scaling 🐳 🐳 🐳

Nowadays a lot of solutions exists so as to freely host small application, such as Firebase, Netlifly, S3, Heroku, Github Pages. They give us the power of massive production in few seconds by scaling automatically (with some cost of course...).

I have been playing with Clever Cloud for side projects and I wanted to share with you how to deploy a Vuejs Application with HTTPS redirection.
Before continuing, I'm not sponsored by Clever Cloud at all. I'm just happy to share this.

Let's start a 1960 funky groovy music:

dancing

Generate the beast with vue-cli πŸ₯

If you already have an application, jump to the next section.
To generate the Vue application, make sure to have vue-cli installed:

$ npm install -g @vue/cli
## To check if it is installed
$ vue --version
Enter fullscreen mode Exit fullscreen mode

The vue application created in this example is the default template from vue-cli:

$ vue create super-vue-app
Enter fullscreen mode Exit fullscreen mode

Follow the setup, open project workplace, run the application and go to your favorite browser the URL http://localhost:8080

 $ cd super-vue-app
 $ npm run serve
Enter fullscreen mode Exit fullscreen mode

To anticipate the production stage, let's build the application with:

$ npm run build
Enter fullscreen mode Exit fullscreen mode

When a vue app is built with the vue-cli-service build command, it creates a production-ready bundle in the dist folder which will be used later.

Setup express ⚑️

The goal is to serve the application from express and properly access the corresponding route. A single-page application is based on a single file index.html and it uses HTML5 History API to manage navigation.
However, by default, a server bypasses index.html and will serve the location requested.
A middleware called connect-history-api-fallback is solving the issue by redirecting all the requests to the index.html.

Install server dependency:

$ npm install express connect-history-api-fallback dotenv --save
Enter fullscreen mode Exit fullscreen mode

After creating the file server.js, the following code must be added to run an Express server:

require("dotenv").config();
var express = require("express");
var app = express();
var history = require("connect-history-api-fallback");

// Synchronize vuejs history mode
app.use(
  history({
    // Enable logging
    verbose: true
  })
);

// Serve all the files in the '/dist' directory
app.use(express.static("dist"));

app.listen(process.env.PORT, "0.0.0.0", function() {
  console.log("My super vue app is listening on port 8080");
});
Enter fullscreen mode Exit fullscreen mode

To run the server, add a new start command to the package.json:

{
  scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "start": "node server.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

To check if the server is working locally, let's run the command and the logger should print My super vue app is listening on port 8080.

$ npm run start
Enter fullscreen mode Exit fullscreen mode

Visit the page http://localhost:8080/about, make a refresh and it should work!

Now it's time to set up the automatic HTTPS redirection, there are 2 possible ways provided by the Clever Cloud documentation:

  • Use a middleware called express-sslify
  • Create our middleware and read the X-Forwarded-Proto header.

For this example, the second option was used. The following code was added just before the app.listen.

// Force HTTPS redirection
app.use(function(req, res, next) {
  if (
    req.secure ||
    req.headers["x-forwarded-proto"] === "https"
  ) {
    return next();
  } else {
    return res.redirect("https://" + req.headers.host + req.url);
  }
});
Enter fullscreen mode Exit fullscreen mode

The application is ready to be deployed. The code is available on Github: https://github.com/steevepay/vue-clever-app

CLI & Deployment πŸš€

Clever Cloud is providing a CLI called clever-tools to speed up deployment. First, install the CLI:

$ npm install -g clever-tools
## Command to check if everything is working
$ clever version
Enter fullscreen mode Exit fullscreen mode

By running the following command, it will open the browser to perform a login/registration on Clever Cloud:

$ clever login
Enter fullscreen mode Exit fullscreen mode

You will be provided 2 environment variables CLEVER_TOKEN and CLEVER_SECRET. This variable can be added in your .bashrc or be exported in your terminal:

$ export CLEVER_TOKEN=...
$ export CLEVER_SECRET=...
Enter fullscreen mode Exit fullscreen mode

Clever Cloud needs to be initialized to receive the application. To achieve so, use the clever create command to define an application.

$ clever create super-vue-app --type node
Enter fullscreen mode Exit fullscreen mode

After running the command, a success message appears: Your application has been successfully created!, it is good news.
To build the application for production the command npm run build needs to be executed. Clever cloud has a deployment life cycle and environment variable hooks can be injected to run commands at a specific time. In our case, CC_POST_BUILD_HOOK is used because it is executed just after the "npm install" where dependencies are fetched. Here is the command:

$ clever env set CC_POST_BUILD_HOOK "npm run build"
Enter fullscreen mode Exit fullscreen mode

Lastly, the following command is used to deploy:

$ clever deploy
Enter fullscreen mode Exit fullscreen mode

Logs are printed in the terminal and the npm commands are executed in the order: install, build and start! Now visit the application at the domain returned by the following command:

$ clever domain
Enter fullscreen mode Exit fullscreen mode

Congratulations, you have just deployed your first app on Clever Cloud πŸ‘

🎁 Bonus: if you need more details about the deployment status:

$ clever status
Enter fullscreen mode Exit fullscreen mode

🎁 A second bonus: to stop the application:

$ clever stop
Enter fullscreen mode Exit fullscreen mode

🎁 A third bonus: If something went wrong during deployment, you can run the following command to restart. It may show more details about errors:

$ clever restart
Enter fullscreen mode Exit fullscreen mode

Conclusion 🎢

Lately, I find it easy to create and deploy applications, POC or anything you want to build. Using tools that make the deployment easy are really valuable as they save lots of your time and energy.

And as always, feel free to reach out to me! 😊

Thanks for reading!

* funky groovy music fades out... 🎢 *

groovy dance

Top comments (0)