How to display console.log results from API requests

一个人想着一个人 提交于 2019-12-12 03:36:34

问题


//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: ');
});

This is my output:

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

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.

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?


回答1:


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

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)

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

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)             
         });
    });

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

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);
        });

    });

Hope it helps




回答2:


In your controller/Factory, make sure you have a callback function to get the response from the server.

$scope.postData = function(){
    $http.post('/', {data: someData})
    .then(function(response){
        console.log(JSON.stringify(response.data));
});


来源:https://stackoverflow.com/questions/35712389/how-to-display-console-log-results-from-api-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!