I've just installed node.js on my computer running Win7(64bit).
The problem is that when I run a simple hello-world application it is running (as confirmed by console.log() and me pushing the code to OpenShift where it works just fine) but when I try to load the page in localhost:1337 it just keeps on loading (eventually times out).
I've no idea what to check, since firewall is not blocking node and I'm not running anything that would block the port.
Here's the server code.
#!/bin/env node
// Include http module.
var http = require("http");
//Get the environment variables we need if on OpenShift
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 1337;
http.createServer(function (request, response) {
request.on("end", function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(port, ipaddr);
console.log('It works');
console.log('IP : ' + ipaddr + '\nPort : ' + port);
Any help is appreciated, thank you.
edit
Here's a screenshot of commandline output.
http://i.stack.imgur.com/GGaLD.png
The node server is hanging as you need to always call response.end.
I believe that listening to the end event on the request is causing the timeout. If you remove it will work.
Related
I have been trying without success to use http module in my Node.js endpoint to do a simple http get.
I have followed the various tutorials to execute the get within my intent, but it keeps failing with getaddrinfo ENOTFOUND in the cloudwatch log.
It seems like I am preparing the url correctly, if I just cut and past the url output into the browswer I get the expected response, and its just a plain http get over port 80.
I suspect that maybe the Alexa hosted lambda doesn't have permission necessary to make remote calls to non-amazon web services, but I don't know this for sure.
Can anybody shed any light? FYI this is the code in my lambda:
var http = require('http');
function httpGet(address, zip, zillowid) {
const pathval = 'www.zillow.com/webservice/GetSearchResults.htm' + `?zws-id=${zillowid}` + `&address=${encodeURIComponent(address)}&citystatezip=${zip}`;
console.log ("pathval =" + pathval);
return new Promise(((resolve, reject) => {
var options = {
host: pathval,
port: 80,
method: 'GET',
};
const request = http.request(options, (response) => {
response.setEncoding('utf8');
console.log("options are" + options);
let returnData = '';
response.on('data', (chunk) => {
returnData += chunk;
});
response.on('end', () => {
resolve(JSON.parse(returnData));
});
response.on('error', (error) => {
console.log("I see there was an error, which is " + error);
reject(error);
});
});
request.end();
}));
}
host: pathval is incorrect usage of the Node.js http module. You need to provide the hostname and the path + query string as two different options.
An example of correct usage:
host: 'example.com',
path: '/webservice/GetSearchResults.htm?zws-id=...',
(Of course, these can be variables, they don't need to be literals as shown here for clarity.)
The error occurs because you're treating the whole URL as a hostname, and as such it doesn't exist.
I suspect that maybe the Alexa hosted lambda doesn't have permission necessary to make remote calls to non-amazon web services
There is no restriction on what services you can contact from a within a Lambda function (other than filters that protect against sending spam email directly to random mail servers).
I'm trying to use a simple post request on a route on top of a mongo DB.
my js file (I combined the router with the app) looks like:
var express = require('express');
var app = express();
var router = express.Router();
app.use(express.static('public'));
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server;
var url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
//Close connection
//db.close();
}});
router.post('/', function(req, res){
res.send('Got a POST request');
});
app.listen(27017,function(){
console.log("Server started successfully at Port 27017!");
});
on my html file I simple have a section like this (yes, my post request doesn't do much for now):
$.ajax({
method: "POST",
url: "localhost:27017/test/",
});
I can't seem to get it to work, my console keeps throwing: "[Error] Failed to load resource: unsupported URL (localhost:27017/test/, line 0)"
at me, and when I try to browse directly to the url via my browser I'm getting a "Cannot GET /test/" message.
What am I doing wrong?
Sharing what worked for me in the end:
1. Changed the app to listen to 3000 (or any other port that my DB server wasn't listening to). Thanks TomG.
2.changed router.post to app.post (you can use expressing routing but I had a mistake there).
I'm new to socket.io and have been able to get many examples from different tutorials working correctly on my localhost. Now I need help getting it to work on my website. I've been browsing support forms for days with no luck. Any help would be appreciated. Here is what I've done so far...
I exported the code (which was working on my localhost) to my web server (hosted by https://ifastnet.com/) using FileZilla FTP Client and did the same "npm init", "npm install express --save", "npm install socket.io --save", "node app.js" procedure on putty SSH that I used on my CMD when I was able to get it to work on my localhost.
When I go to my website I keep getting "net::ERR_CONNECTION_RESET" in the browser console (google chrome) when I use
var socket = io.connect('http://31.22.4.6:1122');
on the client side.
I get "404 (Not Found)" in the browser console when I use
var socket = io();
I've tried many solutions with no luck
My code is below. Thanks in advance for the help.
server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen();
// server.listen(1122, "31.22.4.6");
app.get('/', function(req, res) {
res.sendfile(__dirname + '/client/index.html');
});
console.log("server started");
io.on('connection', function(socket) {
console.log("connection made");
socket.emit('news', {
hello: 'world'
});
socket.on('my other event', function(data) {
console.log(data);
});
});
client
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
// var socket = io.connect('http://localhost');
// var socket = io.connect('http://31.22.4.6:1122');
var socket = io();
// var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Are you using https://ifastnet.com. It doesn't appear that you have access to run node on their servers nor do you have access to serve content on port 1122.
You'll need a service that provides you with ssh, something like Amazon. They have a free-tier service for you to try out a Ubuntu virtual machine for a few months if you want to try before you buy.
I have an automation script in CasperJS controlling a PhantomJS headless browser that logs into a site, enters data over multiple pages / form.
From the same physical server, I have PHP/MySQL serving up a CRM client website. On the CRM site, I want to have the ability to:
Trigger the remote CasperJS script to go browse a remote site and log in and fill out forms
Read the output stream (i.e. "Page 1 complete, page 2 complete" ,etc)
Display the status updates to the client user as the CasperJS script is executing
I am thinking that socket.io is the ticket here. But, I am I going about this all wrong? I am trying to avoid having a selenium server running. I checked this answer on SO but I am not looking for screenshots, I'm looking for the console output from CasperJS to be displayed in the client website.
I had a similar task once and concocted a solution using local Express.js server with Socket.io.
You would launch this server with node.js and then pass tasks to it from PHP by making POST requests to http://127.0.0.1:9000 (I used the excellent Requests library).
Here's a simplified version of my script:
var fs = require("fs");
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
var iosocket;
// Express middleware to get variables from POST request
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// Create websocket connection
io.on("connection", function(socket){
console.log('io.js connection');
iosocket = socket;
});
// Receieve task from external POST request
app.post("/scrape", function(req, res){
res.send("Request accepted");
// Url to parse
var url = req.body.url;
// Variable to collect data from scraper
var data = [];
// Launch scraping script
var spawn = require('child_process').spawn,
child = spawn('/path/to/casperjs', ['/path/to/scrape/script.js', url]);
console.log("Spawned parser");
// Receieve data from script
child.stdout.on('data', function (data) {
var message = data.toString();
data.push(message);
// Send data to the web client
iosocket.emit("message", message);
});
// On error
child.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
// On scraper exit
child.on('close', function (code) {
console.log("Scraper exited with code: " + code);
//
// Put data into a file or a database, for example
//
fs.writeFileSync("path/to/file/results_" + (new Date()).getTime() + ".json", JSON.stringify(data));
});
});
// Bind app to port # localhost
server.listen(9000, "127.0.0.1");
Solution with CasperJS/Phantomjs server is interesting, however people pointed out that it leaks memory, which probably won't be happening if you run short-lived CasperJS scripts.
I noticed serious performance issue when piping a response from a service B to a client through server A. I found out this issue while investigation performance one one of our node server that mainly does proxying. We are using http-proxy for proxying, and at first I thought it was http-proxy that was slow, but I narrowed the issue to that simple snippet of code, which simply use http.get to make a request to another server and return the response to the client.
Server A (proxy):
/*jslint node:true nomen:true vars:true*/
var http = require('http');
function outputMs() {
'use strict';
var d = new Date();
var n = d.getUTCMilliseconds();
return n;
}
http.createServer(function (req, res) {
'use strict';
var startTime = outputMs();
var options = {
hostname: '10.0.1.114',
port: 3001,
path: '/test'
};
if (req.url.indexOf('/pipe') > -1) {
http.get(options, function (proxyRes) {
// This is slow!!!
proxyRes.pipe(res);
proxyRes.on('data', function (chunk) {
console.log('data:' + (outputMs() - startTime));
console.log('received:' + chunk);
});
proxyRes.on('end', function () {
console.log('end:' + (outputMs() - startTime));
});
});
} else {
var data;
http.get(options, function (proxyRes) {
// This is fast!!!
proxyRes.on('data', function (chunk) {
console.log('data:' + (outputMs() - startTime));
data += chunk;
});
proxyRes.on('end', function () {
console.log('end:' + (outputMs() - startTime));
res.end(data);
});
});
}
}).listen(3000);
Server B:
/*jslint node:true nomen:true vars:true*/
var http = require('http');
http.createServer(function (req, res) {
'use strict';
res.end('Hello World');
}).listen(3001);
The request event seems to take the same time in both case, but the client receive the response a lot slower. Testing using curl:
Using pipe:
C:\github\mecs> curl -s -w "%{time_total}\n" -o /dev/null http://54.209.35.253:3000/pipe
0.265
C:\github\mecs> curl -s -w "%{time_total}\n" -o /dev/null http://54.209.35.253:3000/pipe
0.265
Not using pipe:
C:\github\mecs> curl -s -w "%{time_total}\n" -o /dev/null http://54.209.35.253:3000
0.047
C:\github\mecs> curl -s -w "%{time_total}\n" -o /dev/null http://54.209.35.253:3000
0.063
Both server are running on AWS on micro-instance, running Ubuntu Server 12.04.1 LTS and node.js 0.10.21. I reproduced the issue with node 0.10.20 and 0.8.24, and on Ubuntu Server 12.04.2 and 13.10. The issue is not observed on Windows.
Does anyone experience the same problem? Any work around?
Thanks a lot for your help...
You probably need to do something like socket.setNoDelay([noDelay]). Not sure how to do that with the http module though. My guess is one end is waiting for socket close since its a small message, and when it times out, you see the response.
The setNoDelay() must be called on the client request, and first time I tried it was on my proxied request.
server.on('connection', function (socket) {
'use strict';
console.log('server.on.connection - setNoDelay');
socket.setNoDelay(true);
});
I was having similar error. I was able to get response much faster after I set agent in request options with keepAlive=true of agent.