简体   繁体   中英

How do I store user action data? i.e userID date/time and action code

The action code being whichever page the user interacts with

please refer to javascript/nodejs code below.

for example at each router.get() if a user is accesing this i need to store his/her userID the dateTime and an action code matching to each router.

 router.get('/', function(request, response) { response.sendFile(path.join(__dirname + '/eventlist.html')); }); router.get('/watchlist', function(request, response) { response.sendFile(path.join(__dirname + '/watchlist.html')); }); router.get('/search', function(request, response) { response.sendFile(path.join(__dirname + '/search.html')); }); app.post('/search', function(req,res){ let inputContent = req.body.srchterm; var sequelize = require('./db'); sequelize.query('SELECT * FROM main_table WHERE jobname = :jobname OR jobstream = :jobstream OR workstation = :workstation ', { replacements: { jobname: inputContent, jobstream: inputContent, workstation: inputContent }, type: sequelize.QueryTypes.SELECT } ) .then(function(searchib) { console.log(searchib); if (searchib == "") { res.send(srcharray); } else { var srcharray = []; searchib.forEach(function(items){ console.log('displaying srchadata'); srcharray.push ({workstation: items.workstation, jobstream: items.jobstream, jobdate: (items.jobdate.toLocaleString('en-GB', { timeZone: 'GMT'})), jobname: items.jobname }); console.log(srcharray); }); // return response.json(srcharray); res.send(srcharray); } }); }); app.use('/', router); 

您需要使用中间件 ,并且在中间件中 ,您需要从用户那里获取令牌,您可以在其中解码用户令牌以获取用户数据,然后使用时间戳存储该令牌。

Create your own middleware ( storeUserActionDataMiddleware ) to store the data like below:

function storeUserActionDataMiddleware(req, res, next) {
    let data = {
        userId: 42 /* get userId somehow */,
        dateTime: new Date(),
        actionCode: `${req.method} ${req.originalUrl}`,
    };

    console.log({ data });

    // Store user action data here
    // store(data);

    // then execute routes
    next();
}

If you want to store the data only for specific router, then use the middleware at the top of the router like:

// only specific router will store user action data
router.use(storeUserActionDataMiddleware);

router.get(/* ... */)
router.post(/* ... */)

Or, if you want to store the data application wide, then use the middleware at the top of the app like:

// all routes will store user action data
app.use(storeUserActionDataMiddleware);

app.use(/* ... */)
app.get(/* ... */)
app.post(/* ... */)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM