Node.js 如何始终监听 Rest api?

huangapple go评论67阅读模式
英文:

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:

https://nodejs.org/api/events.html

huangapple
  • 本文由 发表于 2020年1月6日 23:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定