如何显示API请求的console.log结果 [英] How to display console.log results from API requests

查看:207
本文介绍了如何显示API请求的console.log结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Express
var express = require('express');
var server = express();

//Steam
var SteamWebAPI = require('steamwebapi').SteamWebAPI;
SteamWebAPI.setAPIKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXX');

//Steam - Recently Played Games
server.get('/games', function (req, res) {
    SteamWebAPI.getRecentlyPlayedGames('76561198190043289', 5, function(response) {
        res.json(response.response.games);
    });
});

//Localhost
server.listen(3000, function () {
    console.log('Server: ');
});

这是我的输出:

[{"appid":730,"name":"Counter-Strike: Global Offensive","playtime_2weeks":620,"playtime_forever":4016,"img_icon_url":"69f7ebe2735c366c65c0b33dae00e12dc40edbe4","img_logo_url":"d0595ff02f5c79fd19b06f4d6165c3fda2372820"}]

好吧,如果您访问网站: https://steamid.io/,请输入名称,然后按按钮查找",您就会获得自己的ID.我该怎么做?是否可以使该用户输入其名称/ID并获得信息.这样,我只有一个可以输入用户的用户,他可以根据他的请求获取文本,图像.

Ok, if you go to website: https://steamid.io/ ,you enter your name, press button 'lookup', and you get your IDs. How do I make that? Is it possible to make that user enter his name/id and he get informations. This way I'm only one who can enter user, and he gets text, images according to his request.

就像,这些天,我对Steam Web Api有了很多了解,但是我仍然没有弄清楚如何显示数据.一些很酷的人帮助了我,但是我们使用了Angular,但我仍然不熟悉它,我打算这几天学习Ember.js,但是有其他选择吗?

Like, I learned a lot about Steam Web Api these days but I still haven't figured out how to display data. Some cool guy helped me, but we used Angular, I'm still not familiar with it, I'm planning to learn Ember.js these days, but are they any alternatives?

推荐答案

如果您想以自己的示例为例,则似乎错过了前端和后端的部分.

If you wanna do as your example, it seems you miss to do the front part, and some back end too.

首先,您必须创建一个表单.在您的情况下,仅要求输入用户ID和提交按钮的输入就足够了.然后,您已在html文件中创建了表单,并使用Express框架提供的render函数(

First you have to create a form. In your case, just an input to ask the user ID and a submit button could be enough. Then you have created your form in a html file, render it with the render function gave by Express framework (http://expressjs.com/en/4x/api.html#app.render)

以及html表单上的简短提示( http://www.w3schools.com/html /html_forms.asp )

And a light reminder on the html form (http://www.w3schools.com/html/html_forms.asp)

但是您必须将其呈现为特定的URL,例如

But you have to render it, to a specific URL, for example

    // Render your basic form
    // The form is in the form.html file
    // By default, views have to be placed into the view or views folder, I don't know exactly no more
    server.get('/ask_user_id', function (req, res) {
         app.render('form', function(err, html){
             if(err) return res.send(err);
             else    return res.send(html)             
         });
    });

因此,如果继续执行localhost:3000/ask_user_id,则将呈现表单

So if you go on localhost:3000/ask_user_id your form will be rendered

第二,当用户将其ID写入您的输入并提交表单时,您必须检索这些信息,然后调用您的Steam API.因此,如果您的表单操作是POST,并且要提交的Url是/user_infos,并且用户ID名称的输入是userId,则将是代码.

Second when a user write its ID into your input and submit the form, you have to retrieve theses info, and then call your steam API. So, if the action of your form is a POST, and the Url to submit is /user_infos, and the input for user ID's name is userId, here will be the code.

    // Render your basic form
    // The form is in the form.html file
    // By default, views have to be placed into the view or views folder, I don't know exactly no more
    server.get('/ask_user_id', function (req, res) {
         app.render('form', function(err, html){
             if(err) return res.send(err);
             else    return res.send(html)             
         });
    });

    // You have to have a route that handle your form submission
    server.post('/user_infos', function (req, res) {

        // All your form submitted is in your req.body
        // So retrieve the userID
        var _userID = req.body.userId;

        // Then sent the variable to the SteamAPI
        SteamWebAPI.getRecentlyPlayedGames(_userID, 5, function(response) {
            return res.json(response.response.games);
        });

    });

希望有帮助

这篇关于如何显示API请求的console.log结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆