5

This is my setup.

  • I have WordPress installed on domain root folder mydomain.com
  • I have my nodejs server listening to port 3000 and installed in mydomain.com/server/ folder
  • I do not have access to use virtualhosts or change apache config

Here is my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^server/(.*)?$ http://127.0.0.1:3000/$1 [P,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

I still can't access my nodejs server from browser or do a call to it from my script (whether its ajax or socket.io)

Please if anyone got a work around for this problem, I've been trying to solve it without any success.

EDIT: This is my server.js

var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var db = require('./db');
var operators = new Array;

// socket.io connection
io.on('connection', function(socket){
    socket.emit('login', { operators: operators });
});

// Request chat button
app.get('/get_button/:key', function(req, res){
    db.connect(function(err){ if (err){ throw err; } else { console.log('DB connected'); }});
    db.query('select * from `wp_oboxchat_keys` where `key` = `' + req.params.key + '` limit 1', function(err, result) {
        if (err) {
            throw err;
        } else {
            res.sendFile(path.join(__dirname, '../', 'app/widget.html'));
            console.log('DB successful query: get_button');
        }
    });
});

// Listen to server
server.listen(3000, function(){ console.log('Server started.'); });

The server runs without any errors on my console with node server.js but its not accessible from a web browser.

2
  • is it a http server listening on port 3000 or socket server? Commented Feb 26, 2018 at 16:15
  • NodeJS /socket.io server listening on 3000. I edited my question Commented Feb 26, 2018 at 16:31

2 Answers 2

3

Resolved.

in my server.js I had to do this:

var server = app.listen(3000);
var io = require('socket.io').listen(server);

And then in my .htaccess I used mod rewrite as following: (I had my nodeserver in /server/ directory relative to my domain root (where wordpress is installed)

RewriteEngine On
RewriteBase /
RewriteRule ^server/(.*)?$ http://127.0.0.1:3000/$1 [P,L]

That's it. I hope this helps anyone who comes across similar issue.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but can't access nodejs app from the browser. URL is not working mydomain.com/node/myproject
3

This helped in my case:

So I had to configure it for a sub-domain. Say subdomain.domain.com was linked to the app/ folder inside the public_html/ folder. I used terminal access to run the NodeJS server(on port 3000) using pm2 inside the app/ folder. In the public_html/app/ folder I added a .htaccess file with the following code inside it:

RewriteEngine on
RewriteRule (.*) http://0.0.0.0:3000/$1 [P,L]
DirectoryIndex disabled

Works like a charm. Added notes:

  1. You can have a .htaccess file inside every folder. So the public_html/test/ folder was running a React app so it had it's own .htaccess file independent of the .htaccess file one directory up.

  2. The address in the .htaccess file can either have 127.0.0.1:3000 or localhost:3000 too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.