How to implement a visit counter into my website? - web

So, I'm hosting a Discord bot on repl.it and I'm using UptimeRobot to keep it running and I thought it'd be cool if I added like a visit counter to see how many times UptimeRobot opened it.
Here is the code for gFex.js (basically the server):
var http = require('http');
http.createServer(function (req, res) {
res.write("gFex is alive.");
res.end();
}).listen(8080);
I tried searching on subreddits and Google but couldn't find anything, is this even possible to implement?

Related

I need my Discord Bot handling GET and POST requests for an external website

I want my Discord Bot handling GET and POST requests, to control some features of my Bot with a dashboard. Knowing that my Bot is hosted in a different server than my future dashboard.
Someone talked about setting a RESTFul API but after some researches I don't really know how to do it and if it will really help me. I can't say if another ways are possible.
I'm using node.js and the library discord.js
You can use express to do different things for GET and POST
Use POST for the webserver requests as the browsers use GET.
Note: the bot would run this to serve the webserver
var express = require('express');
var app = express();
var data = '{"example":{"online":true,"status":"200"}}';
app.get('/', function(req, res) {
res.send('<html><head><script>window.location.href = \'https://google.com/\'</script></head><body>You shouldn\'t be here! <a href=\'https://google.com\'>Exit</a></body></html>');
/** Redirect the browser to google with window.location.href
* Change this to your site */
});
app.post('/', function(req, res) {
/** You could add some auth code here but
* if your sending it all to the client there isn't much of a difference
* because people could read it from the website. */
res.type('json');
res.json(data);
/* Webserver --> Bot */
res.end();
});
app.listen(8080);
console.log('API Online');
You will have to use setInterval(() => {}, 15000); to update the data variable and you need to parse the data on the client.
XMLHttpRequest should work well enough for that

Is it bad practice to wrap express app within socketio connection?

I'm making a webgame, and if I have a route that looks like:
app.post('/create_account', (req, res) => {
var email = req.body.email
var pass = req.body.pass
res.json({response: "created"})
})
Anyone can post data to mywebsite.com/create_account using postman, or curl or something, and my website will start creating account for them even though they are not even on my webpage.
I found an interesting workaround, and I wanted to know if this is safe, or even a good idea. Basically I wrap my app routes within a socket.io connection:
io.on('connection', function(socket) {
app.post('/create_account', (req, res) => {
//code goes here
})
})
I tested it, and it seems that this way you can only post to /create_account if you are actually connected to the webpage.
What (if any) are the disadvantages to doing this? If this is a bad idea, whats's a better way to prevent people from posting data if they aren't on my site.
#Ruslan's suggestion about CSRF tokens is sound and probably your best option, wrapping everything in socket.io seems like too much complexity. Here's a library that does that: https://github.com/expressjs/csurf

Scraping from Facebook

I have a challenge I'm running into and cannot seem to find an answer for it anywhere on the web. I'm working on a personal project; it's a Node.js application that uses the request and cheerio packages to hit an end-point and scrape some data... However, the endpoint is a Facebook page... and the display of its content is dependent upon whether the user is logged in or not.
In short, the app seeks to scrape the user's saved links, you know, all that stuff you add to your "save for later" but never actually go back to (at least in my case). The end-point, then, is htpps://www.facebook.com/saved. If, in your browser, you are logged into Facebook, clicking that link will take you where the application needs to go. However, since the application isn't technically going through the browser that has your credentials and your session saved, I'm running into a bit of an issue...
Yes, using the request module I'm able to successfully reach "a" part of Facebook, but not the one I need... My question really is: how should I begin to handle this challenge?
This is all the code I have for the app so far:
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', (req, res) => {
// Workspace
var url = 'https://www.facebook.com/saved';
request(url, (err, response, html) => {
if (err) console.log(err);
res.send(JSON.stringify(html));
})
})
app.listen('8081', () => {
console.log('App listening on port 8081');
})
Any input will be greatly appreciated... Currently, I'm on hold...! How could I possibly hit this end-point with credentials (safely) provided by the user so that the application could get legitimately get past authentication and reach the desired end-point?
I don't think you can accomplish that using request-cheerio module since you need to make a post request with your login information.
A headless browser is more appropriate for this kind of project if you want it to be a scraper. Try using casperJs or PhantomJs. It will give you more flexibility but it's not a node.js module so you need to make a step further if you want to incorporate it with express.
One nodeJs module I know that can let you post is Osmosis. If you can make .login(user, pw) to work then that'll be great but I don't think it can successfully login to facebook though.
API if possible would be a much nicer solution but I'm assuming you already looked it up and find nothing in there for what you are looking for.
My personal choice would be to use an RobotProcessAutomation. WinAutomation, for example, is a great tool for manipulating web and scraping. It's a whole new different approach but it can do the job well and can be implemented faster compared to programmatically coding it.

Simple web page will not run on xbox one edge browser

I am starting to learn html, css, and nodejs, and after writing a couple of very basic web pages I tried to run them on my computer, and view the page on my xbox-one with edge, but for each one (no matter how simple) it always says "Application is not supported, The webpage tried to start an application, such as an email program, that is not supported on xbox". I've tried with using the node html library as well as the express library, with and without html/css.
One code that would not work on the xbox:
server.js
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("hello world!");
})
var PORT = 1021;
server.listen(PORT);
console.log("Server is running on 192.168.0.15:", PORT, '\n');
I can't find anything online, so any help would be greatly appreciated
Well after a lot of trial and error I found out that it was a very simple problem, I was putting 192.168.0.15:12345 into the address bar but it wanted http://192.168.0.15:1021.

how to find this web address in node.js?

I have a question about express.js. Recently, I'm writing a small project. but how to route to "http://localhost:3000/upload/t/t" like this. I can only navigate to localhost:3000/upload
Just make Get Request as per below.
var app = express();
app.get(['/upload/t/t'], function (req, res) {
//Write your code.
})

Resources