Search code examples
mysqlexpressarchitecturesystem

What is the best approach for providing the recent activity in a system


I need to save the recent activity of a user in my system. Any idea how I can implement this? Links to any articles? Im using ExpressJS running on node as the server.

Thanks in advance.


Solution

  • It depends which actions should be saved as user activity.
    To handle GET / POST requests:

    1. Create MySQL database named «activity» with the table «requests» in MySQL Workbench like this:

    Database

    1. Install an official MySQL connector by typing

       npm install @mysql/xdevapi --save --save-exact
      
    2. Open your index.js and put something like this:

       const express = require('express');
       const mysqlx = require('@mysql/xdevapi');
      
       const app = express();
      
       function log(url) {
         mysqlx.getSession('dbLogin:dbPassword@localhost:33060')
       .then(session => {
         session
           .getSchema('activity')
           .getTable('requests')
           .insert([ 'username', 'url' ])
           .values([ 'User', url ])
           .execute();
       });
       }
      
       app.get('/', (req, res) => {
         log(req.url);
         res.sendFile(__dirname + '/index.html');
       });
      
       app.get('/info', (req, res) => {
         log(req.url);
         res.sendFile(__dirname + '/info.html');
       });
      
       app.listen(3000);
      

    For more detailed information like mouse / keyboard events, you can send POST request with AJAX (see this answer for details) or use WebSockets