//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?
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
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));
});
Related
I've made a website where you fill out a form and then it saves the form data into a JSON file. The data is shown on the site like this:
After that, I send the data in an email. But the email is formatted like this:
How do I get the email to have the same format as the website does? I suspect that the formatting changes where I save the data to the JSON file and then read it again. But I'm not sure and if it is I don't know how to fix it.
Here is the code:
var fs = require('fs')
var formidable = require("formidable")
var util = require('util')
var mailFunctions = require('./mailFunction.js');
'use strict';
function processAllFieldsOfTheForm(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received the data:\n\n');
res.end(util.inspect( {
fields: fields,
files: files
}));
});
}
function processFormFieldsIndividual(req, res) {
var fields = [];
var form = new formidable.IncomingForm();
form.on('field', function (field, value) {
fields.push([field, value]);
});
// Display data to user
form.on('end', function() {
var objectToJSON = JSON.stringify({
fields: fields
}, null, 2)
fs.writeFile('C:/Users/admin/Desktop/Node.js/test/source/json/answers.json', objectToJSON, (err) => {
if (err) {
console.log(err);
console.log('Error while writing file')
throw err;
}
})
res.writeHead(200, {
'content-type': 'text/plain'
});
res.write('received the data: \n\n');
res.end(util.inspect( {
fields: fields
}));
});
// Returns data inside file
form.on('end', function() {
fs.readFile('C:/Users/admin/Desktop/Node.js/test/source/json/answers.json', (err, data) => {
if (err) {
console.log(err)
console.log('Error while reading file')
throw err;
}
else {
mailFunctions.makeMail(data)
}
});
});
form.parse(req);
}
I'm also kinda new to node.js so there is probably so useless/bad code in there.
You didn't include the only relevant part of your source code (the mailFunction.js file) so it's impossible to tell you what's wrong with it. (Also, what's with the screenshots? Are we supposed to use photoshop to fix it? If developers has lost the ability to copy and paste code then there is no hope.)
Now, it's also impossible to tell you how to make an email that looks like your website when we don't know how your website looks like, isn't it, and all that can be said here is a very general advice.
You need to use some templating system to produce HTML (I assume it's HTML but it's also possible that all you need is plain text - again, it's impossible to tell from your question) and you need to make sure that that HTML is correctly formatted for emails - which is quite different than formatting for websites, something that a lot of people don't know.
Then you need to correctly send the HTML email along with all of the attachments because you cannot reliably reference images and other assets hosted on your servers.
There are good modules on npm that can help you with all of those steps. For sending the mail you can use Nodemailer:
https://www.npmjs.com/package/nodemailer
To use templating with Nodemailer, see:
https://community.nodemailer.com/2-0-0-beta/templating/
To send the email it's best to use a transactional email service like Mandrill, Mailgun or Amazon SES if you don't want getting blocked by your ISP and your mail going to spam folders of your users. See:
http://www.mandrill.com/
https://www.mailgun.com/
https://aws.amazon.com/ses/
All of those are supported by Nodemailer.
To know how to format the actual HTML and CSS for your emails to look like you want, see those tutorials:
https://templates.mailchimp.com/getting-started/html-email-basics/
http://blog.mailgun.com/building-html-email-and-workflow-tips/
http://blog.mailgun.com/transactional-html-email-templates/
I'm writing code that would fetch some JSON data and pass it on to a new view with the given data.
The view is called /newbook2 and I want to pass information such as book name, author name and so on to the view from a JSON file called through an API.
var s_book_name;
var s_author_name;
var s_isbn13_id;
var s_publisher_name;
var s_category_id;
var error="";
router.post('/searchbook', function(req, res){
var isbn=req.body.isbn;
var url="http://isbndb.com/api/v2/json/63JEP95R/book/"+isbn;
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if(body.data[0].title!=null)
s_book_name=body.data[0].title;
if(body.data[0].author_data[0]!=null)
s_author_name=body.data[0].author_data[0].name;
if(body.data[0].isbn13!=null)
s_isbn13_id=body.data[0].isbn13;
if(body.data[0].publisher_name!=null)
s_publisher_name=body.data[0].publisher_name;
if(body.data[0].subject_ids[0]!=null)
s_category_id=body.data[0].subject_ids[0];
}
else error="Book not found. Please enter the information manually."
});
res.redirect('/newbook2');
});
However, the information insn't yet loaded in my view. It seems like a common problem with asynchronous calls. However, I'm new to nodejs and would appreciate any help on how to fix it.
router.get('/newbook2', function(req, res){
res.render('newbook2', {title: 'Add New Book',s_book: s_book_name, s_author: s_author_name, s_isbn13: s_isbn13_id ,s_publisher: s_publisher_name , s_category: s_category_id});
});
You need to use promises for this. Here is some sample code for you to refer to make the POST call.
router.post('/newBook', function(req, res, next) {
console.log('Please add your here to get the data from server');
Obj.then(function() {
res.send('You got the success response from the server');
});
});
res.send('Your Book has been successfully added.'); will only execute after you get the success response from the server.
Please go through the concept of promises for more detail.
This is my code to post data
$scope.res=function(fina){
$scope.total= $scope.output;
$scope.finals=angular.copy($scope.fina);
$http.post('/submit', {order: $scope.finals,total: $scope.total}).success(function(data){
alert(data);
$scope.finals=data;
});
};
//load_groups();
$http.get('http://localhost:3070/submit').success(function(data){
alert("here too");
$scope.finals=data;
});
Can I post order and total in the same post? If yes how can I access it in get??
This is my app.get code. How do I send total in this?
app.get('/submit', function (req, res){
//TODO check that the data is valid before just using it
return res.json( req.session.order);
});
I have a an exports function I'm calling that should return a json array of draft results. In the route below in app.js, when I console.log draft_results, I get undefined
app.get('/draft-results', function(req, res) {
var draft_results = fantasy.getDraftResults(req, res);
console.log(util.inspect(draft_results, false, null));
//looks in views folder by default
res.render('draft-results', {
draft_results: draft_results
});
});
In my other file, this is the function that should be returning the json array. If i console.log draft, the data is there.
exports.getDraftResults = function(req, res, cb) {
oauth.get(
"http://fantasysports.yahooapis.com/fantasy/v2/league/" + conf.LEAGUE_ID + "/draftresults?format=json",
req.user.accessToken,
req.user.tokenSecret,
function(e, data, resp) {
if (e) console.error(e);
data = JSON.parse(data);
var draft = data.fantasy_content.league[1].draft_results;
res.json(draft);
}
);
};
I feel like I am returning the data incorrectly, and I can't seem to find any other good examples out there. Could someone please assist?
getDraftResults() is asynchronous. That means the results it generates occur sometime later. Thus, it cannot return its results directly from the function like you are trying to use.
It is unclear what you want to be doing here. Inside of getDraftResults() you are creating a JSON response back to the web request that started all this. That, in itself would be fine and will work as you have it (except the error handling is missing).
But, in your app.get() handler, you have completely different code that seems to thing that getDraftResults() is going to return a value (it has no return value at all) and then you will later use that return value.
So, if you just want getDraftResults to make a JSON response to the original web request, it's already doing that and you can remove the rest of what you have in the app.get() handler. If that's not really what you want to do and you want to use the response from getDraftResults() inside of the app.get() handler, then you will have to change the design of both functions and likely pass a callback to getDraftResults() so the callback can supply the asynchronous response and you can then continue the rest of the app.get() functionality in that callback.
If you're trying to do the latter, then here's a scaffolding (I don't know exactly what you're trying to accomplish so I can't be too detailed here):
app.get('/draft-results', function(req, res) {
fantasy.getDraftResults(req, function(err, draft_results) {
if (err) {
// send some sort of error response here
console.error(err);
return;
}
console.log(util.inspect(draft_results, false, null));
//looks in views folder by default
res.render('draft-results', {
draft_results: draft_results
});
});
});
exports.getDraftResults = function(req, cb) {
oauth.get(
"http://fantasysports.yahooapis.com/fantasy/v2/league/" + conf.LEAGUE_ID + "/draftresults?format=json",
req.user.accessToken,
req.user.tokenSecret,
function(e, data, resp) {
if (e) {
console.error(e);
cb(e);
return;
}
data = JSON.parse(data);
var draft = data.fantasy_content.league[1].draft_results;
// send results back to caller
cb(null, draft);
}
);
};
So I'm selecting Activities from the mongodb and populating User for each.
var query = Activity.find(query).populate("user");
return query.sort({created:"desc"}).exec(function(err, activities) {
debugger;
if (!err) {
return res.json(activities);
} else {
res.status(400).json(err);
}
});
As you can see I have a debugger; breakpoint is there, When I'm pring activities it prints an array of activities with the user object populated.
Also when I'm calling something like activities[0].toJSON() I get everything good!
But the response comes back with the user property empty !
I looked into the source of express.response.json(OBJ) and saw this line:
var body = JSON.stringify(val, replacer, spaces);
val is my activities
When calling JSON.stringify(activities) it will create a json with an empty user field.. any suggestions ?
Try the lean option. That gives back plain JS objects with no mongoose weirdness. Also, your error handling seems a little awkward, can be simplified.
var query = Activity.find(query).populate("user");
query.sort({created:"desc"}).lean().exec(function(err, activities) {
if (err) return res.status(400).json(err);
res.json(activities);
});
I would go even further, not hard-coding error sending in routes but simply passing along via if (err) return next(err) to error-handling middleware defined elsewhere in your app. You can still set the status, then use detection in your middleware, something like this:
app.use(function(err, req, res, next){
err.status = err.status || 500;
res.status(err.status).json(err);
});