-2

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.

lp_nave
  • 244
  • 3
  • 17

1 Answers1

1

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

razor3x
  • 118
  • 6
  • This explains the back end side of things, Thanks @razor3x but if im using react for the front end , the routes in the front end aren't actually sent to the back end with the rest api call right? So unless I send the route with the api call and save it in the database like you clearly explained, Is there any other way to get the route of the front end to the database? – lp_nave Aug 02 '20 at 03:18