[英]how to call webapi at server startup in NodeJS
我已經創建了一個get Web api,我想在服務器啟動時為其執行某些特定任務。 我用過這種方法
var request = require('request');
request('http://localhost:3000/api/jobs/asd', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
}
)
但是我得到的狀態碼為403。所以我無法在服務啟動時訪問我的API。 請為我建議正確的解決方案。
感謝在廣告中
您可以將代碼放入http回調中,如下所示:
http.createServer(app).listen(3000, () => {
//your code here
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.