0

我正在尝试连接到本地服务器上的 js 文件。我希望能够输入外部 IP 地址和端口并让它在外部运行应用程序。目前可以在本地执行此操作。这是我的 server.js 文件的代码:

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');*/

目前我可以从外部连接到服务器,但我最终会进入默认主页,这只是一小段文本。我希望能够输入外部 IP 地址和端口,并让它立即开始运行我的应用程序,而无需浏览目录。如果这有任何帮助,我也在运行 apache。

修复:这行代码代替了 http.createServer 函数使其工作

var server = http.createServer(app).listen(8080);
4

1 回答 1

0

您可以像这样调整/创建您的 apache 配置:

<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>

你应该把它放在你的 apache conf.d 目录(通常是 /etc/httpd/conf.d)中的一个像“nodejs.conf”这样的文件中。

编辑:注意 - 如果您使用默认的 apache 配置,它位于 conf 目录中并命名为 httpd.conf。您可以在其中编辑 VirtualHost 条目以匹配上面的内容。

于 2014-08-06T18:16:22.587 回答