I am trying to connect to a js file on a local server. I want to be able to put in the external ip address and the port and have it run app externally. It is currently possible to do this locally. Here is my code for the server.js file:
var express = require('express');
var app = express();
var mysql = require('mysql');
var connectionpool = mysql.createPool({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'development',
port: 3306,
connectionLimit: 50
});
var UAParser = require('ua-parser-js');
var bodyParser = require('body-parser');
// set the static files location /public/img will be /img for users
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
})); // pull information from html in POST
require('./config/signup.js')(app, express, connectionpool, UAParser);
//Setup for external access
var http = require('http');
http.createServer(function(req, res){
//res.writeHead(200, {'content-type': 'text/plain'});
//res.end('It works');
}).listen(8080);
// Start app on port 8080
/*app.listen(8080);
console.log('Rest Demo Listening on port 8080');*/
At the moment I can connect to the server externally but I end up at the default homepage which is just a small bit of text. I want to be able to enter the external ip address and the port and have it start to run my app instantly without have to navigate through directory’s. I am also running apache if this is of any help.
FIXED: This line of code instaed of the http.createServer function made it work
var server = http.createServer(app).listen(8080);
You can adjust/create your apache configuration like so:
<VirtualHost *:80>
ServerName (SERVER NAME HERE)
ServerAlias (SERVER ALIAS HERE)
ErrorLog "/var/log/httpd/nodejs-error.log"
CustomLog "/var/log/httpd/nodejs-access.log" common
#######################
# Redirect to node.js #
#######################
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
You should place this in your apache conf.d directory (usually /etc/httpd/conf.d) in a file like 'nodejs.conf'.
Edit: Note - if you are using the default apache config, it lives in the conf directory and is named httpd.conf. You can edit the VirtualHost entry in there to match above.