在真实服务器上运行nodejs,expressjs,socket.io应用程序无法正常工作(Running nodejs, expressjs, socket.io application on a

编程入门 行业动态 更新时间:2024-10-28 14:25:37
在真实服务器上运行nodejs,expressjs,socket.io应用程序无法正常工作(Running nodejs, expressjs, socket.io application on a real server not working)

我能够在我的本地机器http://localhost:3000上成功运行nodejs,expressjs,socket.io和mongodb appplication

现在,我已经在支持nodejs的真实服务器上上传了应用程序但是如何使用端口3000运行应用程序? 所以www.mywebsite.com/MyApp:3000不起作用:(我必须在港口运行吗?

你能帮我么?

这是我的服务器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'; });

编辑:如果我使用端口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

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

最满意答案

要使用现有节点/快速代码在生产环境中运行您的应用程序,您需要导航到http://www.mywebsite.com:3000/MyApp (假设您的防火墙允许端口3000)

您可以通过更改代码的这一部分来更改端口:

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

Web的标准端口是80.因此您可以将其更改为

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

然后像往常一样使用您的网址 - http://www.mywebsite.com/MyApp

注意:如果您的服务器上有另一个使用此端口的Web服务器,那么您将遇到问题,因为在端口空闲之前它不会监听。 您必须首先禁用在端口80上侦听的其他Web服务器。 非特权用户(非root用户)也无法侦听1024以下端口上的套接字。

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.

更多推荐

本文发布于:2023-08-03 14:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   应用程序   器上   真实   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!