简体   繁体   English

Node.js未在服务器中运行

[英]Nodejs not running in server

I am trying to launch nodejs in my server. 我正在尝试在服务器中启动nodejs。 this is my node.js code: 这是我的node.js代码:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, 'xx.xx.xx.xx');
console.log('Server running at http://xx.xx.xx.xx:8080/');

this is my /etc/nginx/nginx.conf 这是我的/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    worker_connections  1024;
}


http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server{
        location / {
            proxy_pass http://xx.xx.xx.xx:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}
#   

I also deleted a few lines from default.conf because it was showing up error nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) and following @ rednaw's answer 我还从default.conf删除了几行,因为它显示了错误nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)并遵循@ rednaw的回答

I am trying to run node.js in my xx.xx.xx.xx server. 我正在尝试在xx.xx.xx.xx服务器中运行node.js。 Now it's just showing this at http://xx.xx.xx.xx/ and This site can't be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED 现在它只是显示这个HTTP://xx.xx.xx.xx/This site can't be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED This site can't be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED at http://xx.xx.xx.xx:8080/ This site can't be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED位于http://xx.xx.xx.xx:8080 /

You have to mention the port number ie not in use to listen. 您必须提及端口号, 即不用于监听。

Do it some thing like this---- 做这样的事情----

var http =  require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8083);

In my scenario the port number 8080 was already in use....So i used 在我的场景中,端口号8080已经在使用中。

another port number like 8083 ....It works... 另一个端口号,例如8083 ...。

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

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