Node.js server just keeps loading with no result. - javascript

I have been trying to get my server to work but when I send post data it just keeps loading and no results are given. Here is my noen.js file.
var http = require('http');
var url = require('url');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
// user told us their name in the GET request, ex: http://host:8000/?name=Tom
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {sys.puts(stdout)}
exec ("casperjs test.js " + queryData.name + '\n');
} else {
response.end("Contact Admin - Not Working\n");
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(1213);
Can anyone help me fix this? When I go to
127.0.0.1:8000/?name=tom
I get no response the page just goes into a long loading loop

There is no response.end in case if is true so then response "never" ends.
write at bottom of the if
response.end("something");
And you will get the response;
For get the output of the process to the response:
https://stackoverflow.com/a/3944751/3018595
var http = require('http');
var url = require('url');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
// user told us their name in the GET request, ex: http://host:8000/?name=Tom
var exec = require('child_process').exec;
exec ("casperjs test.js " + queryData.name + '\n',function(err, stdout, stderr) {
response.end(stdout);
});
} else {
response.end("Contact Admin - Not Working\n");
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(1213);

The reason your browser is keep waiting because you are not ending your response. You have to call response.end to let your server complete the response otherwise it will keep thinking that the response is not complete yet. I added a line in your if statement and tested your code and it is working perfectly fine.
added line ** response.end("Request processed successfully...\n");**, assuming that you need to display a different message in case your "else" statement.
I tested url http://:1213/?name=tom
var http = require('http');
var url = require('url');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
// user told us their name in the GET request, ex: http://host:8000/?name=Tom
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {sys.puts(stdout)}
exec ("casperjs test.js " + queryData.name + '\n');
response.end("Request processed successfully...\n");
} else {
response.end("Contact Admin - Not Working\n");
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(1213);

Related

Using a sh script to send node js server requests with specified query parameters on the URL

I'm a little lost right now on how to get started on my current project. I really just need some help at just getting it going and I should be able to take it from there. I have some experience with JavaScript, php, and mysql but almost nothing in node js.
I have a .sh script file that looks something like this:
curl localhost:3000/signup?user=jack\&height=6\&time=0
curl localhost:3000/arm?left=0.000000\&right=0.000000\&time=0 --cookie "USER=jack"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=jack"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=jack"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=jack"
I want to send my node js server requests with these specified query parameters in the shell but I have no clue how to do so. Here is my node js code thus far:
const { exec } = require('child_process');
var script = exec('myscript.sh' ,
(error, stout, stderr) => {
console.log(stdout);
console.log(stderr);
if ( error !== null ) {
console.log(`exec error: ${error}`);
}
});
var http = require('http');
var url = require('url');
http.createServer(function(req, res) {
var q = url.parse(req.url, true).query;
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end("user " + q.first + " height " + q.second);
}).listen(3000);
When I go to localhost:3000 I can see "user undefined height undefined" and when I type "node file.js" into my command line then I get the script to start echoing each line in the linux command line... But how do I put the two together? How would I parse a script or use that script to send the requests that I need to my node js server? I really just have no idea where to begin on this project. Would things be easier simply using php and mysql? Thanks for any help I can get with this. Thanks.
You have to wait for your http server before your can make requests to it:
const http = require('http');
const url = require('url');
const { exec } = require('child_process');
// create http server
const server = http.createServer(function (req, res) {
// parse query parameter
var q = url.parse(req.url, true).query;
// write http header to client
res.writeHead(200, {
'Content-Type': 'text/html'
});
// send back body repsonse
res.end("user " + q.first + " height " + q.second);
});
// wait for the http server to start
server.on("listening", () => {
// do requests to the defined http server above
const script = exec('myscript.sh', (error, stout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error) {
console.log(`exec error: ${error}`);
}
});
});
server.listen(3000);
Dont forget to make your .sh script executable chmod +x myscript.sh
#!/bin/bash
curl "http://localhost:3000/signup?user=jack&height=6&time=0"
curl "http://localhost:3000/arm?left=0.000000&right=0.000000&time=0" --cookie "USER=jack"
curl "http://localhost:3000/echo?dist=9.220000&time=10" --cookie "USER=jack"
curl "http://localhost:3000/line?l1=1&l2=1&l3=1&time=20" --cookie "USER=jack"
curl "http://localhost:3000/other?ir=0&time=30" --cookie "USER=jack"

Send data to Node.js Clients

I am trying to build a Node.js App to Monitor some Raspberry Pi's.
Since those Raspberries don’t have a static IP, they send an UDP Broadcast every 5 seconds.
I'm able to catch that Broadcast with Node.js, but I'm failing to trigger a new function to notify the Node.js Clients.
I tried WebSockets, ServerSendEvents and Socket.io.
I'm able to use Example Code and they work just fine.
But I'm not Experienced enough to build a function which will send data to the clients.
Node.js App:
// ==============================================================================================================
// ===== Dependencies ===========================================================================================
// ==============================================================================================================
var dgram = require('dgram');
var http = require('http');
var url = require("url");
var path = require("path");
var fs = require("fs");
// ==============================================================================================================
// ===== HTTP Serv ==============================================================================================
// ==============================================================================================================
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname, filename = path.join(process.cwd(), uri);
var contentTypesByExtension = {
'.html': "text/html",
'.css': "text/css",
'.js': "text/javascript",
'.svg': "image/svg+xml"
};
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
var headers = {};
var contentType = contentTypesByExtension[path.extname(filename)];
if (contentType) headers["Content-Type"] = contentType;
response.writeHead(200, headers);
response.write(file, "binary");
response.end();
});
});
});
// ==============================================================================================================
// ===== HeartBeat Broadcast ====================================================================================
// ==============================================================================================================
var bcast = dgram.createSocket('udp4');
bcast.on('message', function (message) {
console.log("Triggered: UDP Broadcast");
// If UDP Broadcast is received, send message/data to client.
});
bcast.bind(5452, "0.0.0.0");
// ==============================================================================================================
// ===== Start Server ===========================================================================================
// ==============================================================================================================
server.listen(80);
console.log("Static file server running/\nCTRL + C to shutdown");
EDIT:
I think I did not explain myself accurate enough.
I do not want to send a UDP message back.
This UDP Broadcast should fire an (Node.js) event, which should update the html and display the raspberry pi (whom send the UDP Package) as online.
EDIT:
In documentation from official page of nodejs (DOCUMENTATION):
var socket = require('socket.io')(http);
var bcast = dgram.createSocket('udp4');
bcast.bind(5452, "0.0.0.0");
bcast.on('message', function (message, remote) {
////if message is an Object pushed into Buffer////
message = message.toString('utf8');
socket.emit("HTML_Update", message);
//////////////////////////////////Solution for unedited question//////////////////////////
// var msgBuffer = Buffer.from(message.toString(); //creating a buffer //
// bcast.send(msgBuffer, 0, msgBuffer.length, remote.port, remote.address, (err) => { //
// bcast.close(); //
// }); //sending message to remote.address:remote.port (like localhost:23456) //
// //
// **build a function which will send data to the clients** //
//////////////////////////////////Solution for unedited question//////////////////////////
});
"If message is an Object pushed into Buffer" - lets say that one of the RPI turned on and started sending UDP message, what should the message pass to server so server can pass it to display: mac address only because if it sends something You can be sure its on, if it does not send its off simple as that. Also to show that change on client You should initialize TCP sockets on server to pass info to servers web page to update content on html with jquery.
Now here is the HTML java script part (I personally make main.js file and write all java script into it and use import it as src into html). Using jquery in main.js:
$(document).ready(function() {
var time = new Date();
var rpi = {
"list" : ["mac1", "mac2", "mac3"],
"time" : [time.getTime(), time.getTime(), time.getTime()],
"label" : ["label_ID1", "label_ID2", "label_ID3"]};
var socket = io.connect('http://your_server_address:80');
setInterval( function(){
for (var i = 0; i <= 2; i++){
if((rpi.time[i] + 10000) < time.getTime()){
$(rpi.label[i]).text("RPI " + rpi.list[i] + " is DOWN");
}
}
}, 5000);
socket.on("HTML_Update", function(data){
for (var i = 0; i<=2; i++) {
if (data.toString().equals(rpi.list[i])) {
$(rpi.label[i]).text("RPI: "+ rpi.list[i] + " is UP");
rpi.time[i] = time.getTime();
}
}
});
}
If You put text label in html to show if specific rpi is up or down this part of code works in this scheme:
Multiple RPI + Server - RPI sends UDP data with mac to server. Server device is used to receive data and show it on any device as web page and change data if RPI is UP/DOWN.

Receive text message with Twilio node module and respond with a text message

var http = require('http');
var twilio = require('twilio')(ACCOUNT_SID, AUTH_TOKEN);
var qs = require('querystring');
http.createServer(function (req, res) {
var body = '';
req.setEncoding('utf8');
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
var data = qs.parse(body);
var jsonString = JSON.stringify(data);
var jsonDataObject = JSON.parse(jsonString);
// log the received message
console.log(jsonDataObject.Body);
twilio.messages.create({
to:'MY_PHONE_NUMBER',
from:'TWILIO_NUMBER',
body:'Hello World'
}, function(error, message) {
if (error) {
console.log('There was an error.')
console.log(error.message);
}
});
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end();
});
}).listen(1337, '127.0.0.1');
console.log('TwiML servin\' server running at http://127.0.0.1:1337/');
I'm trying to use the Twilio node module to receive a text message and in turn respond to that text message once received. There seems to be no problem receiving the message as I'm able to log the body. But, I get a 401 Authenticate error when I try and respond to that message. I'm using ngrok to expose my localhost so I can hook it into Twilio's API. Please see below:
Where am I going wrong here?
Twilio developer evangelist here.
You actually don't need to use the REST API in order to reply to an incoming message to a Twilio number. You can, in fact, respond to the incoming HTTP request with TwiML that describes the message in response.
To do this, you need to use the <Message> verb. In your application, this would look like:
First, just require the twilio module without the account credentials:
var twilio = require("twilio");
Then, respond to the incoming request with TwiML, like so:
req.on('end', function() {
var data = qs.parse(body);
var jsonString = JSON.stringify(data);
var jsonDataObject = JSON.parse(jsonString);
// log the received message
console.log(jsonDataObject.Body);
var twiml = new twilio.TwimlResponse();
twiml.message("Hello world");
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
Let me know if this helps at all.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
var server = app.listen(80, function () {
var host = server.address().address
var port = server.address().port
console.log(" web app listening at http://%s:%s", host, port)
})
app.post('/txt', urlencodedParser,(req, res) => {
const twiml = new MessagingResponse();
twiml.message('Finally Twilio works!');
res.status(200);
res.send(twiml.toString());
});
Under your phone number in the console.
You can click webhooks and change it to the http://"putyourserverhere"/txt
This will automatically text back the inbound user.
Enjoy. Make sure you have the newest version of twilio installed.

Node.js sample code not working

I'm trying to run some simple codes of node.js, this hello world works without a problem:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Este node.js criou um servidor\n");
});
server.listen(8000);
But when I try to run this one that should be simple enough, the browser (tried IE and chrome) stays loading for a long time and then gives a timeout. What could be the problem?
var http = require("http"),
fs = require("fs");
http.createServer(function (request, response) {
request.on('end', function () {
if (request.url == '/') {
fs.readFile('test.txt', 'utf-8', function (error, data) {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
data = parseInt(data) + 1;
fs.writeFile('test.txt', data);
response.end('This page was refreshed ' + data + ' times!');
});
} else {
response.writeHead(404);
response.end();
}
});
}).listen(8000);
By the way, I've created the test.txt file in the same folder as the code and it have only the number 1 inside it.
The end request never launch, because the request is finished when the server is called. Remove that line and ready like this:
var http = require("http"),
fs = require("fs");
http.createServer(function (request, response) {
// request.on('end', function () {
if (request.url == '/') {
fs.readFile('test.txt', 'utf-8', function (error, data) {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
data = parseInt(data) + 1;
fs.writeFile('test.txt', data);
response.end('This page was refreshed ' + data + ' times!');
});
} else {
response.writeHead(404);
response.end();
}
// });
}).listen(8000);
The request object from a HTTP handler is an instance of readable stream, which will not emit the end event when in non-flowing mode. If an end event is expected, then the stream must be resumed..
If you aren't going to collect the body of the request, then you don't need to listen for the end event at all. You can just write the response:
http.createServer(function(req, res) {
if (request.url == '/') {
fs.readFile('test.txt', 'utf-8', function (error, data) {
res.writeHead(200, {'Content-Type': 'text/plain'});
data = parseInt(data) + 1;
fs.writeFile('test.txt', data);
res.end('This page was refreshed ' + data + ' times!');
});
} else {
res.writeHead(404);
res.end();
}
}).listen();
Otherwise, the stream can converted to flowing mode by either of these:
req.resume();
req.on('data', function(chunk) {});
The way that it seems to be structured is quite different from what I have seen otherwise. You shouldn't need the request.on structure. Generally, you could using something like this:
var http = require("http");
var Start = function(){
var onRequest = function(request, response){
response.writeHead(200, {"Content-Type" : "text/plain" });
response.write("HEllo World");
response.end();
}
http.createServer(onRequest).listen(8888);
}
exports.Start = Start;
The reason for the exports is so that you can start it form another file (which is good if you are going for the suggested modular design. As to loading files - your way of loading may work, but the problem is that it will only server text file - trying to use an html file will fail miserably, and you probably want to be serving html files as well, along with .js and .css files as well (and anything else, for example pictures). Therefore, please refer to my answer, which is the very long one with plenty of code, in the folllowing link: Click Here.

Nodejs output -Domain name not found

Technically this is my first try in nodejs and frankly I am not sure if I am doing it right. I am creating a local server that will stream the output from a distant server. However, when I run my code and I enter a URL in the browser, the program fails with the following message:
events.js:45
throw arguments[1]; // Unhandled 'error' event
^
Error: ENOTFOUND, Domain name not found
at IOWatcher.callback (dns.js:74:15)
The URL I used was: 127.0.0.1:9000/http://www.yahoo.fr. And in the browser I had the following message:
No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this web page later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
Any help is greatly appreciated.
Here is the code:
var base, dest, node_client,
count = 0,
url = require('url'),
util = require('util'),
http = require('http'),
http_client = require('http'),
request = require('request'),
events = require('events'),
httpProxy = require('./lib/node-http-proxy'),
data_emitter = new events.EventEmitter();
httpProxy.createServer(9000, 'localhost').listen(8000);
http.createServer(function (req, res) {
if(!count)
{
base = url.parse(req.url).pathname;
node_client = http_client.createClient(80, base);
count++;
} else {
dest = req.url.substr(1, req.url.length -1);
}
request = node_client.request("GET", dest, {"host": base});
request.addListener("response", function (response) {
var body = "";
response.addListener("data", function (data) {
body +=data;
});
response.addListener("end", function () {
var out = JSON.parse(body);
if(out.length > 0) {
data_emitter.emit("out", out);
}
});
});
// request.close();
var listener = data_emitter.addListener("data", function(out) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(JSON.stringify(out));
res.close();
});
}).listen(9000);
Wild guess : your browser automatically requests 127.0.0.1:9000/favicon.ico and your program then tries to resolve favicon.ico which obviously fails and makes your program crash before it can send any data for the real request.
Why such tangled code?
This is a scenario where it makes sense to avoid nested callbacks, and use named functions. If you refactor the code, then people are more likely to be help you.
Can you do console.log(out) in your listener callback? Let us know if Node.js has any response data to return.
Well, for any newbie like me in this area, here is how I solved it. It's not clean and can be implemented in better way. Feel free to change, give suggestions.
Code:
var url = require('url'),
http = require('http'),
request = require('request'),
httpProxy = require('./lib/node-http-proxy'),
des = '',
util = require('util'),
colors = require('colors'),
is_host = true;
httpProxy.createServer(9000, 'localhost').listen(8000);
var server = http.createServer(function (req, res) {
var pathname = '';
if(is_host) {
dest = req.url.substr(0, req.url.length -1);
pathname = dest;
is_host = false;
} else {
pathname = req.url.substr(0, req.url.length);
if(pathname.charAt(0) == "/") {
console.log('new request');
console.log(pathname);
pathname = dest + pathname;
}
}
console.log(pathname);
request.get({uri: pathname}, function (err, response, html) {
res.end(html);
});
console.log('fetched from ' + pathname);
});
server.listen(9000);

Categories

Resources