JAVA Developer Training
28. cron,agent 시간별 자동화, 접속자 확인기능 본문
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 ..
확인 가능
결과 예 )
첫줄은 접속한 사이트
버전, 프로그램 확인가능
'트레이닝' 카테고리의 다른 글
30. Spring 게시판 작성 ( Oracle 이미지 연동 ) (0) | 2021.09.16 |
---|---|
29. Oracle 연동 ( 게시판 만들기 ) (0) | 2021.09.14 |
27. 데이터 크롤링 ( node.js, selenium ) (0) | 2021.09.10 |
26. socket.io를 이용한 실시간 채팅 기초 틀 (0) | 2021.09.08 |
25. JAVA spring 이용 (0) | 2021.09.06 |