Configure socket.io on MAMP localhost

755 Views Asked by At

I'm trying to configure a socket.io for a webapp i'm building.

I have successfully installed the dependencies with npm.

This is the index.js that is executed on the server side. (all my public folders, including the index.html, are located in the httpdocs folder)

var express = require("express");
var app = express();
var port = 8080;

app.get("/", function(req, res){
    res.send("Hello world!");
});

app.use(express.static(__dirname + '/httpdocs'));
var io = require('socket.io').listen(app.listen(port));

When I go to http://localhost:8080 I can see the 'Hello world!' in the browser, when I go to http://localhost:8080/socket.io/socket.io.js It shows me the socket.io.js file so that seems to work fine.

On the client side i can't seem to import that socket.io.js with the line below in my index.html

<script src="/socket.io/socket.io.js"></script>

It just redirects to the 404 page and drops the syntax error.

I assume its because my MAMP uses port 80 for the Apache server on the client side. The project runs as a virtual host.

<VirtualHost *:80>
    ServerAdmin *my email*
    DocumentRoot "*document root*/httpdocs/"
    ServerName project.local
    ServerAlias *.project.local
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

Changing the listener port from 8080 to 80 does not fix that problem. I guess I have to use two different ports for bouth.

1

There are 1 best solutions below

2
On

I'm trying to do this as well, and as far as I know you have to either use the official Apache client (something I can't find for Mac, which I use as well) or use a VirtualHost directive, but as far as I know you can't do that on the MAMP WebStart page. Try using another server client, maybe?

(NOTE: If I find the Mac official Apache client, I'll edit this question to add the link on the bottom.)