简体   繁体   English

在真实服务器上运行nodejs,expressjs,socket.io应用程序不起作用

[英]Running nodejs, expressjs, socket.io application on a real server not working

I'm able to successfully run nodejs, expressjs, socket.io and mongodb appplication on my local machine http://localhost:3000 我能够在本地计算机http://localhost:3000上成功运行nodejs,expressjs,socket.io和mongodb应用程序

Now, I have uploaded the application on the real server that supports nodejs but how can I run the application using the port 3000? 现在,我已将应用程序上载到支持nodejs的真实服务器上,但如何使用端口3000运行该应用程序? So www.mywebsite.com/MyApp:3000 doesn't work :( Do I have to run in a PORT? 所以www.mywebsite.com/MyApp:3000不起作用:(我必须在PORT中运行吗?

can you please help me? 你能帮我么?

Here's my server app.js code: 这是我的服务器app.js代码:

var express = require('express');

var  mongoose = require ('mongoose');

var app = express();
app.use('/', express.static('../app/'));
app.use('/bower_components', express.static('../bower_components/'));

var http = require('http').Server(app);
var io = require('socket.io')(http);

//mongodb databse
mongoose.connect ('mongodb://127.0.0.1/mydb', function (err) {
    if (err) {
        console.log (err);
    } else {
        console.log ("Connected to mongodb");
    }
});


io.sockets.on ('connection', function (socket) {
    console.log("hello world, I'm running fine!")
});

http.listen(3000, function () {
    'use strict';
});

EDIT: if I use port 80, I get this error: 编辑:如果我使用端口80,我得到此错误:

 events.js:87
          throw er; // Unhandled 'error' event
                ^
    Error: listen EACCES
        at exports._errnoException (util.js:748:11)
        at Server._listen2 (net.js:1123:19)
        at listen (net.js:1166:10)
        at Server.listen (net.js:1251:5)
      ....etc

To run your app on production using your existing node / express code you would naviagate to http://www.mywebsite.com:3000/MyApp (assuming your firewall allows port 3000) 要使用现有的节点/快速代码在生产环境中运行您的应用,请导航至http://www.mywebsite.com:3000/MyApp (假设防火墙允许端口3000)

You can change the port though to be what ever you want by changing this part of the code: 您可以通过更改以下部分代码,将端口更改为所需端口:

http.listen(3000, function () {
    'use strict';
});

The standard port for web is 80. So you could change this to be Web的标准端口是80。因此您可以将其更改为

http.listen(80, function () {
    'use strict';
});

And then use your url as normal - http://www.mywebsite.com/MyApp 然后照常使用您的网址-http: http://www.mywebsite.com/MyApp

Note: If you have another web server on your server that is using this port already then you are going to have a problem as it will not listen until the port is free. 注意:如果您的服务器上已经有另一个Web服务器正在使用此端口,那么您将遇到问题,因为它将在端口空闲之前不进行监听。 You will have to disabled other web servers listening on port 80 first. 您必须先禁用其他Web服务器,然后才能监听端口80。 Non-privileged user (not root) can't listen on sockets on ports below 1024 either. 非特权用户(非root用户)也无法监听端口号低于1024的套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM