Web 4 Final Project - Going Live with your API

Scroll down to see the notes that go with the video, they may be helpful!

Before starting

// HTTP to HTTPS redirect (only in production environment)
if (process.env.NODE_ENV === 'production') {
  app.enable('trust proxy'); 
  app.use((req, res, next) => {  
    if (req.secure) {
      // Request is already HTTPS, continue to the next middleware
      next();
    }else{
      // Redirect to HTTPS with 301 status code
      res.redirect(301, 'https://' + req.headers.host + req.url);
    }
  });
}

cPanel setup

Log in to cPanel, these instuctions will guide you thorugh the following steps:

Create a subdomain

To create a subdomain:

Create a Node App

In the tools dashboard, scroll down to the Software section and click Setup Node.js App

When you complete these steps, cPanel will create a ./src/server.js file that has a simple Node program that prints 'It works!'. You can see this by opening a browser window and navigating to:

Copy the project files to the live server

Now it's time to copy our code files to the public_html/web4.yourdomain.xxx/ folder on your cPanel server.

Zip the following files from your project:

Use FileManager to upload the zip file to public_html/web4.yourdomain.xxx/

Then right-click on the zip file and choose extract

You won't see the extracted files unless you click the 'reload' button

Configure Node App

Return to the Node application up in cPanel:

Now that we have uploaded the package.json file, you'll see a button to Run NPM Install.

In the browser, navigate to https://web4.yourdomain.xxx/api/

Then try the health-check route at https://web4.yourdomain.xxx/api/health-check

Troubleshooting

Try it out in PostMan

Making changes

If you change the database you can export it from your local machine but you may have to drop all the tables on the live db before you can import the .sql file

If you make changes to the project code