英文:
Node.js How to always listen to Rest api?
问题
Sure, here's the translated content:
嘿,我想要构建我的第一个Node.js应用程序,我想获得一些指导。
我有一个提供定价列表的外部服务的Rest API。
价格每隔几秒钟更新一次,我想监听这个API,并在每次获取新的更改时更新我的数据库。
我来自PHP,例如在这里我可以创建Cron作业并在所有表上执行foreach,但我知道这不是一个明智的主意,我知道Node.js是为此而生的。
我想获取任何关于如何开始构建这样的东西的想法。
谢谢!
英文:
Hey I want to build my first node.js app and I would like to get some guidance.
So I have external service that provide me Rest Api with pricing list.
The pricing update every few seconds, I want to listen to this api and update my database every time I get new changes.
I come from PHP and for example here I can make Cron job and make foreach on all the table, but I know it's not smart idea, and I know node.js made for this.
I would like to get any ideas how to start to build something like that.
Thanks!
答案1
得分: 1
One option would be to use an Event Emitter:
var EventEmitter = require('events')
var ee = new EventEmitter()
ee.on('message', function (text) {
//Here you would update your database
console.log(text)
})
ee.emit('message', 'hello world')//You would call this whenever/wherever the API sends your data
NPM events:
https://www.npmjs.com/package/events
NodeJS Docs:
https://nodejs.org/api/events.html
英文:
One option would be to use an Event Emitter:
var EventEmitter = require('events')
var ee = new EventEmitter()
ee.on('message', function (text) {
//Here you would update your database
console.log(text)
})
ee.emit('message', 'hello world')//You would call this whenever/wherever the API sends your data
NPM events:
https://www.npmjs.com/package/events
NodeJS Docs:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论