简体   繁体   中英

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

Now, I have uploaded the application on the real server that supports nodejs but how can I run the application using the port 3000? So www.mywebsite.com/MyApp:3000 doesn't work :( Do I have to run in a PORT?

can you please help me?

Here's my server app.js code:

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:

 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)

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

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

And then use your url as normal - 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. You will have to disabled other web servers listening on port 80 first. Non-privileged user (not root) can't listen on sockets on ports below 1024 either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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