connect.static() interpret PHP files with node.js and connect - node.js

I successfully configured node with connect and nowjs. Calling localhost:8001/test.html and localhost:8001/chat.html works without any problems. But when calling a PHP-file localhost:8001/test.php my browser wants me to download that file. Obviously connect.static won't interpret the PHP file. Is there any other possibility to get it work for html and php files?
Thanks
var http = require('http');
var connect = require('connect');
var nowjs = require("now");
var app = connect();
app.use(connect.static('/var/www/www.domain.com/htdocs'));
app.use(function(req, res){
res.end();
});
var server = http.createServer(app).listen(8001);
var everyone = nowjs.initialize(server);

Related

Include CSS and JS in HTTP server?

I saw that you can use connect to use serve static files in a Node.js HTTP server like this:
var http = require('http');
var connect = require('connect');
var app = connect().use(connect.static(__dirname + path));
http.createServer(app).listen(8080);
How would I implement this in my current handler?
var http = require("http");
var handler = function(request, response){
// code
}
http.createServer(handler);
Is this even possible? If so, how can I accomplish it?
Unless you want to use an older version of connect that might not function properly, you'd have to install serve-static to do what you're trying to do. See this answer https://stackoverflow.com/a/24347442/5382465
var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')
// Serve up public folder
var serve = serveStatic('public', {'index': ['index.html', 'index.htm']})
// Create server
var handler = http.createServer(function onRequest (req, res) {
serve(req, res, finalhandler(req, res))
})
// Listen
handler.listen(3000)

Connecting Flash to Node via websockets - Crossdomain.xml issue

I am trying to set up a simple websocket server that communicates between a Flash AS3 client and a Node.js backend. The following code is when both the client and server are hosted locally. The code also functions properly when the swf is hosted locally connected to the server hosted on heroku. The socket only fails when both the swf and the server are hosted online.
Node.js Code (Server)
var WebSocketServer = require("ws").Server
var net = require('net');
var http = require("http")
var express = require("express")
var app = express()
var crossdomain = require('crossdomain')
var port = process.env.PORT || 5000;
app.use(express.static(__dirname + "/"))
var xml = crossdomain({ domain: '*' });
app.all('/crossdomain.xml', function (req, res, next) {
res.writeHead(200, { 'Content-Type': 'application/xml; charset=utf-8' });
res.write(xml);
//res.write(new Buffer([0x00]))
res.end();
});
var server1 = http.createServer(app)
server1.listen(port)
console.log("http server listening on " + port)
var wss = new WebSocketServer({server: server1})
wss.on("connection", function(ws) {
console.log("Connected");
ws.on("close", function() {
console.log("websocket connection close")
})
})
Crossdomain file - example.com/crossdomain.xml
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" secure="false"/>
</cross-domain-policy>
AS3 Code (Client) - I'm Using (https://github.com/theturtle32/AS3WebSocket)
import com.worlize.websocket.*;
Security.loadPolicyFile("http://example.com/crossdomain.xml");
var websocket:WebSocket = new WebSocket("ws://example.com", "*", "echo-protocol");
websocket.addEventListener(WebSocketEvent.OPEN, handleWebSocketOpen);
websocket.connect();
var con = false;
function handleWebSocketOpen(event:WebSocketEvent):void {
trace(event);
}
When the client is accessed from the server a request for the crossdomain.xml does occur and it gets returned successfully to the client, but at that point nothing happens anymore. I've been trying to solve this for ages with no luck. I did find that when you return the crossdomain file you need to end it with a 0x00 for it to be properly read, but I haven't found a way to make to that work. I've tried adding res.write(new Buffer([0x00])) to the response from the crossdomain request as well as several other modifications to no avail. These all seem to stop the xml file from being valid anyway.
I would greatly appreciate if anyone could help in getting this to work, I've been struggling with it for an extremely long time and can't quite seem to get this last step working.

Websocket With Socket.io & Angular

I want to create a websocket to add a communication between my angular app and my database. The app shall be able to save a question in a database & to notify the user when somebody answered.
Unfortunately I tried some tutorials which all don't work. I'm totally new to this because I use apache usually. So If you know a "working", very basic (beginner) tutorial, which doesn't require to install lots of additional things like yeoman etc. I would be glad.
In the current tutorial I get the error message:
You are trying to attach socket.io to an express request handler function. Please pass a http.Server instance.
My server.js:
var connect = require('connect'),
serveStatic = require('serve-static'),
socket = require('socket.io');
var server = connect();
server.use(serveStatic(__dirname+'/../client'));
server.listen(8080);
var io = socket.listen(server);
console.log("Server started and listen to http://127.0.0.1:8080");
without
var io = socket.listen(server);
it serves my static page.
This error is on my other approach:
has no method 'use'
My server.js
var connect = require('connect');
var http = require('http');
var serveStatic = require('serve-static');
var app = connect();
var server = http.createServer(app).use(serveStatic(__dirname+'/../client')).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);
As always if you struggle long the solution comes quick after posting. That one works for the beginning:
var connect = require('connect');
var http = require('http');
var app = connect();
var fs = require('fs');
var index = fs.readFileSync(__dirname+'/../client/index.html');
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end(index);
}).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);

Socket.io doesn't correctly attach to http server in express application

I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.

socket.io initialize statement throws error on azure

I have developed a very simple appp in node.js its working fine locally using express and socket.io. But when i deploy this on azure website using git it gives me error. I figure out that line that initialize socket.io throws. Following is my server.js file.
//try this
var http = require('http')
var fs = require("fs");
var express = require("express");
var twitter = require('ntwitter');
var app = express();
var port = process.env.PORT || 1337;
app.get("/", function(request, response){
var content = fs.readFileSync("template.html");
// console.log("Contents");
// console.log(content);
response.setHeader("Content-Type", "text/html");
response.send(content);
});
var server = app.listen(port, function() {
//console.log('Listening on port %d', server.address().port);
});
Following line works locally but on azure gives throws error
var io = require('socket.io').listen(server); //working on localhost
multiple folders named "emitter" that contained the indexof module also had a gitignore file that made git ignore the module, no idea why that was even there, but deleting them fixed the problem
used git add -A --force it solved it for azure and heroku after push. socket.io working perfectly.

Resources