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
- Make sure your database is set up on the live server
- Make sure you have the info from setting up the live db:
- The name of the database on the live server
- The user account that you created in the live database server
- The password that you created for the user account
- Make sure you are redirecting to https
- I should have given you a code snippet to add to app.js
- In case I did not, here it is (put it right after the line that sets up the logging):
// 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 for your app
- Create a Node application
- Copy the project code into the folder for your subdomain
Create a subdomain
To create a subdomain:
- Go to the Tools dashboard in cPanel
- Click on the link to Domains (in the 'Domains' section)
- Click Create a New Domain
- For the Domain text input, enter web4.yourdomain.xxx (replace 'yourdomain.xxx' with your domain name)
- Copy the path to the folder that automatically gets set for the Document Root, it looks like this:
- public_html/web4.yourdomain.xxx
- You'll need this string later
Create a Node App
In the tools dashboard, scroll down to the Software section and click Setup Node.js App
- Set the Node.js version (to the recommended version)
- Click Create Application
- Set the Application mode to Production
- Set the Applicatioin root to the folder that got created when you created the subdomain:
- public_html/web4.yourdomain.xxx
- Set Application start up file to ./src/server.js
- set Passenger log file to public_html/web4.yourdomain.xxx/passenger.log
- Press the Create button
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:
- http://web4.yourdomain.xxx/
- You should see a message that says It works!
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:
- src (folder)
- .env
- package.json
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:
- In the Tools dashboard, scroll to the Software section and click on Setup Node.js app
- Click the pencil icon for the app we created to edit it.
Now that we have uploaded the package.json file, you'll see a button to Run NPM Install.
- Click the buton to Run the npm install
- It can take a few minutes to install the node modules
- It may show an error message when it completes, but this doesn't necessarily mean there's a problem
- Set Environment variables (they will override any variables with the same name that are in the .env file). Click the Add Variable button and create the following variables:
- DB_NAME - set it to the name of the database that you created in cPanel
- DB_USER - set it to the user account that you created for the database in cPanel
- DB_PASSWORD - set it to the password that you created for the user account
- JWT_SECRET - set this to a long randomish string
- You don't need to set the DB_HOST because it's the same as in the .env file (localhost)
- Restart the node app (I think that pressing the Save button restarts the app)
In the browser, navigate to https://web4.yourdomain.xxx/api/
- You should see a Hello World! message
Then try the health-check route at https://web4.yourdomain.xxx/api/health-check
- Hopefully you'll see a message that says it successfully connected to the database
Troubleshooting
- Check the passenger.log file in public_html/web4.yourdomain.xxx/
- Check the error.log in public_html/web4.yourdomain.xxx/src
Try it out in PostMan
- Set the baseUrl variable, which makes it easy to switch all your routes between the dev URLs and the live URLs
- Apply the baseUrl variable to your routes.
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
- Zip the src folder, .env file, and package.json file, then upload and extract on the live server
- Restart the Node application