Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

JAVA Developer Training

28. cron,agent 시간별 자동화, 접속자 확인기능 본문

트레이닝

28. cron,agent 시간별 자동화, 접속자 확인기능

Romenest 2021. 9. 13. 10:39
var express = require('express');
var router = express.Router();

const mongoclient = require('mongodb').MongoClient;
const mongourl  = "mongodb://id318:pw318@1.234.5.158:37017/id318";
const mongodb   = "id318";
const mongocoll = "member9";

//npm install node-cron --save
var cron = require('node-cron');



// http://www.npmjs.com/package/node-cron
// # ┌────────────── second (optional)
// # │ ┌──────────── minute
// # │ │ ┌────────── hour
// # │ │ │ ┌──────── day of month
// # │ │ │ │ ┌────── month
// # │ │ │ │ │ ┌──── day of week
// # │ │ │ │ │ │
// # │ │ │ │ │ │
// # * * * * * *
cron.schedule('*/10 * * * * *', async function(){
    const dbconn = await mongoclient.connect(mongourl);
    const coll   = dbconn.db(mongodb).collection(mongocoll);

    const obj = {
        name : 'aaa1',
        date : new Date()
    }

    coll.insertOne(obj);
    console.log(new Date());
})



module.exports = router;

10초마다 aaa1과 해당 시간을 db에 넣는 작업

 

var express = require('express');
var router = express.Router();


//접속한 사용자의 ip주소 알아내는법
// 127.0.0.1:3000/agent
router.get('/agent',async function(req, res, next) {
  var userAddress = req.headers['x-fowrarded-for']
  ||req.connection.remoteAddress

  var userAgent = req.headers['user-agent'];

  console.log(userAddress);
  console.log(userAgent);

  res.send({ret:1});
});

module.exports = router;

접속자의 프로그램, ex ) chrome, firefox ..

확인 가능

결과 예 ) 

첫줄은 접속한 사이트

버전, 프로그램 확인가능