Before copying the Notes and user authentication code to this server, let's do a little coding to prepare for the move. We know that the Notes and authentication services must access the MySQL instance on localhost using the usernames and passwords given earlier.
Using the approach we've followed so far, this means a pair of YAML files for Sequelize parameters, and changing environment variables in the package.json files to match.
Create a chap10/notes/models/sequelize-server-mysql.yaml file containing:
dbname: notes
username: notes
password: notes12345
params:
host: localhost
port: 3306
dialect: mysql
It was discovered during testing that a simple password such as notes was not acceptable to the MySQL server, and that a longer password was required. In chap10/notes/package.json, add the following line to the scripts section:
"on-server": "SEQUELIZE_CONNECT=models/sequelize-server-mysql.yaml NOTES_MODEL=sequelize USER_SERVICE_URL=http://localhost:3333 PORT=3000 node --experimental-modules ./app",
Then create a chap10/users/sequelize-server-mysql.yaml file containing the following code the following code:
dbname: userauth
username: userauth
password: userauth
params:
host: localhost
port: 3306
dialect: mysql
The passwords shown in these configuration files obviously will not pass any security audits.
In chap10/users/package.json, add the following line to the scripts section:
"on-server": "PORT=3333 SEQUELIZE_CONNECT=sequelize-server-mysql.yaml node --experimental-modules ./user-server",
This configures the authentication service to access the databases just created.
Now we need to select a place on the server to install the application code:
# ls /opt
This empty directory looks to be as good a place as any. Simply upload chap10/notes and chap10/users to your preferred location. Before uploading, remove the node_modules directory in both directories.
That's both to save time on the upload, and because of the simple fact that any native-code modules installed on your laptop will be incompatible with the server.
On your laptop, you might run a command like this:
$ rsync --archive --verbose ./ root@159.89.145.190:/opt/
Use the actual IP address or domain name assigned to the server being used.
You should end up with something like the following:
# ls /opt notes users
Then, in each directory, run these commands:
# rm -rf node_modules # npm install
The simplest way of doing this is to just delete the whole node_modules directory and then let npm install do its job. Remember that we set up the PATH environment variable the following way:
# export PATH=./node_modules/.bin:${PATH}
You can place this in the login script (.bashrc, .cshrc, and so on) on your server so it's automatically enabled.
Finally, you can now run the SQL scripts written earlier to set up the database instances:
# mysql -u root -p <users/mysql-create-db.sql # mysql -u root -p <notes/models/mysql-create-db.sql
Then you should be able to start up the services by hand to check that everything is working correctly. The MySQL instance has already been tested, so we just need to start the user authentication and Notes services:
# cd /opt/users # DEBUG=users:* npm run on-server
> user-auth-server@0.0.1 on-server /opt/users
> PORT=3333 SEQUELIZE_CONNECT=sequelize-server-mysql.yaml node --experimental-modules ./user-server
(node:9844) ExperimentalWarning: The ESM module loader is experimental.
Then log in to the server on another Terminal session and run the following:
# cd /opt/users/ # PORT=3333 node users-add.js
Created { id: 1, username: 'me', password: 'w0rd', provider: 'local',
familyName: 'Einarrsdottir', givenName: 'Ashildr', middleName: '',
emails: '[]', photos: '[]',
updatedAt: '2018-02-02T00:43:16.923Z', createdAt: '2018-02-02T00:43:16.923Z' } # PORT=3333 node users-list.js
List [ { id: 'me', username: 'me', provider: 'local',
familyName: 'Einarrsdottir', givenName: 'Ashildr', middleName: '',
emails: '[]', photos: '[]' } ]
The preceding command both tests that the backend user authentication service is functioning and gives us a user account we can use to log in. The users-list command demonstrates that it works.
You may get an error:
users:error /create-user Error: Please install mysql2 package manually
This is generated inside of Sequelize. The mysql2 driver is an alternate MySQL driver, implemented in pure JavaScript, and includes support for returning Promises for smooth usage in async functions. If you do get this message, go ahead and install the package and remember to add this dependency to your package.json.
Now we can start the Notes service:
# cd ../notes # npm run on-server
> notes@0.0.0 on-server /opt/notes
> SEQUELIZE_CONNECT=models/sequelize-server-mysql.yaml NOTES_MODEL=sequelize USER_SERVICE_URL=http://localhost:3333 PORT=3000 node --experimental-modules ./app
(node:9932) ExperimentalWarning: The ESM module loader is experimental.
Then we can use our web browser to connect to the application. Since you probably do not have a domain name associated with this server, Notes can be accessed via the IP address of the server, such as http://159.89.145.190:3000/.
By now, you know that the drill for verifying Notes is working. Create a few notes, open a few browser windows, see that real-time notifications work, and so on. Once you're satisfied that Notes is working on the server, kill the processes and move on to the next section, where we'll set this up to run when the server starts.